From b47b6cba53ace64d9aebfab42e04a446a90ec139 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 6 Feb 2023 10:30:42 -0800 Subject: [PATCH] use lookup rather than object cache when deleting highlight --- .../Services/DataService/DataService.swift | 1 + .../Mutations/DeleteHighlight.swift | 19 +++++-------------- .../Services/DataService/OfflineSync.swift | 2 +- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 3c640c914..daf417852 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -38,6 +38,7 @@ public final class DataService: ObservableObject { self.networker = networker self.persistentContainer = PersistentContainer.make() self.backgroundContext = persistentContainer.newBackgroundContext() + backgroundContext.automaticallyMergesChangesFromParent = true backgroundContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump if isFirstTimeRunningNewAppBuild() { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift index 286e0524b..c9d08197f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift @@ -5,15 +5,10 @@ import SwiftGraphQL public extension DataService { func deleteHighlight(highlightID: String) { - if let highlight = Highlight.lookup(byID: highlightID, inContext: viewContext) { - deleteHighlight(objectID: highlight.objectID) - } - } + guard let highlight = Highlight.lookup(byID: highlightID, inContext: viewContext) else { return } - private func deleteHighlight(objectID: NSManagedObjectID) { - // Update CoreData + // Update CoreData so view updates immediately viewContext.performAndWait { - guard let highlight = viewContext.object(with: objectID) as? Highlight else { return } highlight.serverSyncStatus = Int64(ServerSyncStatus.needsDeletion.rawValue) do { @@ -25,14 +20,10 @@ public extension DataService { } } - // Send update to server - backgroundContext.perform { [weak self] in - guard let highlight = self?.backgroundContext.object(with: objectID) as? Highlight else { return } - self?.syncHighlightDeletion(highlightID: highlight.unwrappedID, objectID: objectID) - } + syncHighlightDeletion(highlightID: highlightID) } - internal func syncHighlightDeletion(highlightID: String, objectID: NSManagedObjectID) { + func syncHighlightDeletion(highlightID: String) { enum MutationResult { case saved(id: String) case error(errorCode: Enums.DeleteHighlightErrorCode) @@ -63,7 +54,7 @@ public extension DataService { let isSyncSuccess = data != nil context.perform { - guard let highlight = context.object(with: objectID) as? Highlight else { return } + guard let highlight = Highlight.lookup(byID: highlightID, inContext: context) else { return } if isSyncSuccess { highlight.remove(inContext: context) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index 482b38c6e..1c9208240 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -176,7 +176,7 @@ public extension DataService { ) case .needsDeletion: highlight.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) - syncHighlightDeletion(highlightID: highlight.unwrappedID, objectID: highlight.objectID) + syncHighlightDeletion(highlightID: highlight.unwrappedID) case .needsUpdate: if let annotation = highlight.annotation { highlight.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue)