From d00089ea25d3fd086aa487e75de6b852f451b4db Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 6 Feb 2023 13:06:25 -0800 Subject: [PATCH] perform sync within conext perform blocks --- .../DataService/Mutations/ArchiveLink.swift | 11 +++++----- .../Mutations/DeleteHighlight.swift | 13 ++++++------ .../DataService/Mutations/RemoveLink.swift | 15 +++++++------ .../UpdateArticleReadingProgress.swift | 19 ++++++++--------- .../Mutations/UpdateLinkedItemTitle.swift | 21 +++++++++---------- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift index f7a6fe777..3dc992d73 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift @@ -5,16 +5,15 @@ import SwiftGraphQL extension DataService { public func archiveLink(objectID: NSManagedObjectID, archived: Bool) { - guard let linkedItem = backgroundContext.object(with: objectID) as? LinkedItem else { return } - // Update CoreData - backgroundContext.performAndWait { [weak self] in + backgroundContext.perform { [weak self] in guard let self = self else { return } + guard let linkedItem = self.backgroundContext.object(with: objectID) as? LinkedItem else { return } linkedItem.update(inContext: self.backgroundContext, newIsArchivedValue: archived) - } - // Send update to server - syncLinkArchiveStatus(itemID: linkedItem.unwrappedID, archived: archived) + // Send update to server + self.syncLinkArchiveStatus(itemID: linkedItem.unwrappedID, archived: archived) + } } func syncLinkArchiveStatus(itemID: String, archived: Bool) { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift index c9d08197f..98f5bf032 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift @@ -5,22 +5,21 @@ import SwiftGraphQL public extension DataService { func deleteHighlight(highlightID: String) { - guard let highlight = Highlight.lookup(byID: highlightID, inContext: viewContext) else { return } - // Update CoreData so view updates immediately - viewContext.performAndWait { + viewContext.perform { + guard let highlight = Highlight.lookup(byID: highlightID, inContext: self.viewContext) else { return } highlight.serverSyncStatus = Int64(ServerSyncStatus.needsDeletion.rawValue) do { - try viewContext.save() + try self.viewContext.save() logger.debug("Highlight succesfully marked for deletion") } catch { - viewContext.rollback() + self.viewContext.rollback() logger.debug("Failed to mark Highlight for deletion: \(error.localizedDescription)") } - } - syncHighlightDeletion(highlightID: highlightID) + self.syncHighlightDeletion(highlightID: highlightID) + } } func syncHighlightDeletion(highlightID: String) { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift index 3a2b255c2..24fa18ebe 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift @@ -5,23 +5,22 @@ import SwiftGraphQL public extension DataService { func removeLink(objectID: NSManagedObjectID) { - guard let linkedItem = viewContext.object(with: objectID) as? LinkedItem else { return } - // Update CoreData - viewContext.performAndWait { + viewContext.perform { + guard let linkedItem = self.viewContext.object(with: objectID) as? LinkedItem else { return } linkedItem.serverSyncStatus = Int64(ServerSyncStatus.needsDeletion.rawValue) do { - try viewContext.save() + try self.viewContext.save() logger.debug("LinkedItem succesfully marked for deletion") } catch { - viewContext.rollback() + self.viewContext.rollback() logger.debug("Failed to mark LinkedItem for deletion: \(error.localizedDescription)") } - } - // Send update to server - syncLinkDeletion(itemID: linkedItem.unwrappedID) + // Send update to server + self.syncLinkDeletion(itemID: linkedItem.unwrappedID) + } } func syncLinkDeletion(itemID: String) { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift index c980379dd..33711f651 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift @@ -5,24 +5,23 @@ import SwiftGraphQL extension DataService { public func updateLinkReadingProgress(itemID: String, readingProgress: Double, anchorIndex: Int) { - guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: backgroundContext) else { return } - - backgroundContext.performAndWait { [weak self] in + backgroundContext.perform { [weak self] in guard let self = self else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: self.backgroundContext) else { return } linkedItem.update( inContext: self.backgroundContext, newReadingProgress: readingProgress, newAnchorIndex: anchorIndex ) - } - // Send update to server - syncLinkReadingProgress( - itemID: linkedItem.unwrappedID, - readingProgress: readingProgress, - anchorIndex: anchorIndex - ) + // Send update to server + self.syncLinkReadingProgress( + itemID: linkedItem.unwrappedID, + readingProgress: readingProgress, + anchorIndex: anchorIndex + ) + } } func syncLinkReadingProgress(itemID: String, readingProgress: Double, anchorIndex: Int) { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift index 49cb33f89..70cf059e1 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift @@ -5,10 +5,9 @@ import SwiftGraphQL extension DataService { public func updateLinkedItemTitleAndDescription(itemID: String, title: String, description: String, author: String?) { - guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: backgroundContext) else { return } - - backgroundContext.performAndWait { [weak self] in + backgroundContext.perform { [weak self] in guard let self = self else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: self.backgroundContext) else { return } linkedItem.update( inContext: self.backgroundContext, @@ -16,15 +15,15 @@ extension DataService { newDescription: description, newAuthor: author ) - } - // Send update to server - syncLinkedItemTitleAndDescription( - itemID: itemID, - title: title, - author: author, - description: description - ) + // Send update to server + self.syncLinkedItemTitleAndDescription( + itemID: itemID, + title: title, + author: author, + description: description + ) + } } func syncLinkedItemTitleAndDescription(