Skip to content

Commit

Permalink
Strongly retain self to ensure completion block is always called
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed Jul 12, 2023
1 parent 65631ae commit 00d8364
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions WordPress/Classes/Services/JetpackSocialService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ import CoreData
/// - blogID: The ID of the blog.
/// - completion: Closure that's called after the sync process completes.
func syncSharingLimit(for blogID: Int, completion: @escaping (Result<PublicizeInfo.SharingLimit?, Error>) -> Void) {
remote.fetchPublicizeInfo(for: blogID) { [weak self] result in
// allow `self` to be retained inside this closure so the completion block will always be executed.
remote.fetchPublicizeInfo(for: blogID) { result in
switch result {
case .success(let remotePublicizeInfo):
self?.coreDataStack.performAndSave({ context -> PublicizeInfo.SharingLimit? in
self.coreDataStack.performAndSave({ context -> PublicizeInfo.SharingLimit? in
guard let blog = try Blog.lookup(withID: blogID, in: context) else {
// unexpected to fall into this case, since the API should return an error response.
throw ServiceError.blogNotFound(id: blogID)
Expand Down Expand Up @@ -84,7 +85,7 @@ import CoreData
return
}

syncSharingLimit(for: blogID, completion: { [success, failure] result in
syncSharingLimit(for: blogID, completion: { result in
switch result {
case .success:
success?()
Expand Down

0 comments on commit 00d8364

Please sign in to comment.