use lookup rather than object cache when deleting highlight

This commit is contained in:
Satindar Dhillon
2023-02-06 10:30:42 -08:00
parent c4281b545c
commit b47b6cba53
3 changed files with 7 additions and 15 deletions

View File

@ -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() {

View File

@ -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)

View File

@ -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)