From 00d83643bd742114b587a72dc5425b795d73d204 Mon Sep 17 00:00:00 2001 From: David Christiandy <1299411+dvdchr@users.noreply.github.com> Date: Thu, 13 Jul 2023 00:17:34 +0700 Subject: [PATCH] Strongly retain self to ensure completion block is always called --- WordPress/Classes/Services/JetpackSocialService.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/WordPress/Classes/Services/JetpackSocialService.swift b/WordPress/Classes/Services/JetpackSocialService.swift index 7887ad6087a3..153941e2a211 100644 --- a/WordPress/Classes/Services/JetpackSocialService.swift +++ b/WordPress/Classes/Services/JetpackSocialService.swift @@ -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) -> 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) @@ -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?()