From ef93b89a8ec1c952a655fbd2d9d5495598b45ec4 Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Mon, 27 Mar 2023 11:04:24 +0100 Subject: [PATCH 1/2] Fix: update blog instance from the correct thread --- WordPress/Classes/Services/BlazeService.swift | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/WordPress/Classes/Services/BlazeService.swift b/WordPress/Classes/Services/BlazeService.swift index 5360cef45f95..2a1010c81e33 100644 --- a/WordPress/Classes/Services/BlazeService.swift +++ b/WordPress/Classes/Services/BlazeService.swift @@ -33,31 +33,41 @@ import WordPressKit completion: (() -> Void)? = nil) { guard BlazeHelper.isBlazeFlagEnabled() else { - updateBlog(blog, isBlazeApproved: false, completion: completion) + updateBlogWithID(blog.objectID, isBlazeApproved: false, completion: completion) return } guard let siteId = blog.dotComID?.intValue else { DDLogError("Invalid site ID for Blaze") - updateBlog(blog, isBlazeApproved: false, completion: completion) + updateBlogWithID(blog.objectID, isBlazeApproved: false, completion: completion) return } remote.getStatus(forSiteId: siteId) { result in switch result { case .success(let approved): - self.updateBlog(blog, isBlazeApproved: approved, completion: completion) + self.updateBlogWithID(blog.objectID, isBlazeApproved: approved, completion: completion) case .failure(let error): DDLogError("Unable to fetch isBlazeApproved value from remote: \(error.localizedDescription)") - self.updateBlog(blog, isBlazeApproved: false, completion: completion) + self.updateBlogWithID(blog.objectID, isBlazeApproved: false, completion: completion) } } } - private func updateBlog(_ blog: Blog, isBlazeApproved: Bool, completion: (() -> Void)? = nil) { + private func updateBlogWithID(_ objectID: NSManagedObjectID, + isBlazeApproved: Bool, + completion: (() -> Void)? = nil) { contextManager.performAndSave({ context in + + guard let blog = context.object(with: objectID) as? Blog else { + DDLogError("Unable to update isBlazeApproved value for blog") + completion?() + return + } + blog.isBlazeApproved = isBlazeApproved DDLogInfo("Successfully updated isBlazeApproved value for blog: \(isBlazeApproved)") + }, completion: { completion?() }, on: .main) From 20eddd86c7fc3d3856e0dde3b199b055fe2057b5 Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Tue, 28 Mar 2023 15:06:20 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Fix:=20don=E2=80=99t=20call=20completion=20?= =?UTF-8?q?closure=20during=20core=20data=20ops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WordPress/Classes/Services/BlazeService.swift | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/WordPress/Classes/Services/BlazeService.swift b/WordPress/Classes/Services/BlazeService.swift index 2a1010c81e33..1e90c5477ec8 100644 --- a/WordPress/Classes/Services/BlazeService.swift +++ b/WordPress/Classes/Services/BlazeService.swift @@ -3,12 +3,12 @@ import WordPressKit @objc final class BlazeService: NSObject { - private let contextManager: CoreDataStack + private let contextManager: CoreDataStackSwift private let remote: BlazeServiceRemote // MARK: - Init - required init?(contextManager: CoreDataStack = ContextManager.shared, + required init?(contextManager: CoreDataStackSwift = ContextManager.shared, remote: BlazeServiceRemote? = nil) { guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: contextManager.mainContext) else { return nil @@ -28,7 +28,7 @@ import WordPressKit /// /// - Parameters: /// - blog: A blog - /// - completion: Closure to be called on success + /// - completion: Closure to be called on completion @objc func getStatus(for blog: Blog, completion: (() -> Void)? = nil) { @@ -58,16 +58,12 @@ import WordPressKit isBlazeApproved: Bool, completion: (() -> Void)? = nil) { contextManager.performAndSave({ context in - - guard let blog = context.object(with: objectID) as? Blog else { - DDLogError("Unable to update isBlazeApproved value for blog") - completion?() + guard let blog = try? context.existingObject(with: objectID) as? Blog else { + DDLogError("Unable to fetch blog and update isBlazedApproved value") return } - blog.isBlazeApproved = isBlazeApproved DDLogInfo("Successfully updated isBlazeApproved value for blog: \(isBlazeApproved)") - }, completion: { completion?() }, on: .main)