From 4c73f792862afe2db49c48c29f6cda93c313b33e Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 6 Feb 2023 10:46:16 -0800 Subject: [PATCH] avoid using coredata cache in data service network calls handlers --- .../DataService/Mutations/ArchiveLink.swift | 19 +++++++------- .../DataService/Mutations/RemoveLink.swift | 12 ++++----- .../UpdateArticleReadingProgress.swift | 24 +++++++++--------- .../Mutations/UpdateHighlightAttributes.swift | 23 ++++++----------- .../Mutations/UpdateLinkedItemTitle.swift | 25 +++++++++---------- .../Services/DataService/OfflineSync.swift | 11 +++----- 6 files changed, 50 insertions(+), 64 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift index d8412c8a9..f7a6fe777 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift @@ -5,18 +5,19 @@ import SwiftGraphQL extension DataService { public func archiveLink(objectID: NSManagedObjectID, archived: Bool) { - // Update CoreData - 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) + guard let linkedItem = backgroundContext.object(with: objectID) as? LinkedItem else { return } - // Send update to server - self.syncLinkArchiveStatus(itemID: linkedItem.unwrappedID, objectID: objectID, archived: archived) + // Update CoreData + backgroundContext.performAndWait { [weak self] in + guard let self = self else { return } + linkedItem.update(inContext: self.backgroundContext, newIsArchivedValue: archived) } + + // Send update to server + syncLinkArchiveStatus(itemID: linkedItem.unwrappedID, archived: archived) } - func syncLinkArchiveStatus(itemID: String, objectID: NSManagedObjectID, archived: Bool) { + func syncLinkArchiveStatus(itemID: String, archived: Bool) { enum MutationResult { case success(linkId: String) case error(errorCode: Enums.ArchiveLinkErrorCode) @@ -48,7 +49,7 @@ extension DataService { let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync context.perform { - guard let linkedItem = context.object(with: objectID) as? LinkedItem else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: context) else { return } linkedItem.serverSyncStatus = Int64(syncStatus.rawValue) do { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift index 5fd1bbfca..3a2b255c2 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift @@ -5,9 +5,10 @@ import SwiftGraphQL public extension DataService { func removeLink(objectID: NSManagedObjectID) { + guard let linkedItem = viewContext.object(with: objectID) as? LinkedItem else { return } + // Update CoreData viewContext.performAndWait { - guard let linkedItem = viewContext.object(with: objectID) as? LinkedItem else { return } linkedItem.serverSyncStatus = Int64(ServerSyncStatus.needsDeletion.rawValue) do { @@ -20,13 +21,10 @@ public extension DataService { } // Send update to server - backgroundContext.perform { [weak self] in - guard let linkedItem = self?.backgroundContext.object(with: objectID) as? LinkedItem else { return } - self?.syncLinkDeletion(itemID: linkedItem.unwrappedID, objectID: objectID) - } + syncLinkDeletion(itemID: linkedItem.unwrappedID) } - func syncLinkDeletion(itemID: String, objectID: NSManagedObjectID) { + func syncLinkDeletion(itemID: String) { enum MutationResult { case success(linkId: String) case error(errorCode: Enums.SetBookmarkArticleErrorCode) @@ -64,7 +62,7 @@ public extension DataService { let isSyncSuccess = data != nil context.perform { - guard let linkedItem = context.object(with: objectID) as? LinkedItem else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: context) else { return } if isSyncSuccess { linkedItem.remove(inContext: context) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift index b6f218fd2..c980379dd 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift @@ -5,27 +5,27 @@ import SwiftGraphQL extension DataService { public func updateLinkReadingProgress(itemID: String, readingProgress: Double, anchorIndex: Int) { - backgroundContext.perform { [weak self] in + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: backgroundContext) else { return } + + backgroundContext.performAndWait { [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 - self.syncLinkReadingProgress( - itemID: linkedItem.unwrappedID, - objectID: linkedItem.objectID, - readingProgress: readingProgress, - anchorIndex: anchorIndex - ) } + + // Send update to server + syncLinkReadingProgress( + itemID: linkedItem.unwrappedID, + readingProgress: readingProgress, + anchorIndex: anchorIndex + ) } - func syncLinkReadingProgress(itemID: String, objectID: NSManagedObjectID, readingProgress: Double, anchorIndex: Int) { + func syncLinkReadingProgress(itemID: String, readingProgress: Double, anchorIndex: Int) { enum MutationResult { case saved(readAt: Date?) case error(errorCode: Enums.SaveArticleReadingProgressErrorCode) @@ -62,7 +62,7 @@ extension DataService { let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync context.perform { - guard let linkedItem = context.object(with: objectID) as? LinkedItem else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: context) else { return } linkedItem.serverSyncStatus = Int64(syncStatus.rawValue) if let mutationResult = data?.data, case let MutationResult.saved(readAt) = mutationResult { linkedItem.readAt = readAt diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift index ae757c230..5367164bd 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift @@ -4,26 +4,19 @@ import Models import SwiftGraphQL extension DataService { - public func updateHighlightAttributes( - highlightID: String, - annotation: String - ) { + public func updateHighlightAttributes(highlightID: String, annotation: String) { + guard let highlight = Highlight.lookup(byID: highlightID, inContext: backgroundContext) else { return } + backgroundContext.perform { [weak self] in guard let self = self else { return } - guard let highlight = Highlight.lookup(byID: highlightID, inContext: self.backgroundContext) else { return } - highlight.update(inContext: self.backgroundContext, newAnnotation: annotation) - - // Send update to server - self.syncHighlightAttributes( - highlightID: highlightID, - objectID: highlight.objectID, - annotation: annotation - ) } + + // Send update to server + syncHighlightAttributes(highlightID: highlightID, annotation: annotation) } - func syncHighlightAttributes(highlightID: String, objectID: NSManagedObjectID, annotation: String) { + func syncHighlightAttributes(highlightID: String, annotation: String) { enum MutationResult { case saved(highlight: InternalHighlight) case error(errorCode: Enums.UpdateHighlightErrorCode) @@ -58,7 +51,7 @@ extension DataService { let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync context.perform { - guard let highlight = context.object(with: objectID) as? Highlight else { return } + guard let highlight = Highlight.lookup(byID: highlightID, inContext: context) else { return } highlight.serverSyncStatus = Int64(syncStatus.rawValue) do { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift index fc9c54231..49cb33f89 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateLinkedItemTitle.swift @@ -5,9 +5,10 @@ import SwiftGraphQL extension DataService { public func updateLinkedItemTitleAndDescription(itemID: String, title: String, description: String, author: String?) { - backgroundContext.perform { [weak self] in + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: backgroundContext) else { return } + + backgroundContext.performAndWait { [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, @@ -15,21 +16,19 @@ extension DataService { newDescription: description, newAuthor: author ) - - // Send update to server - self.syncLinkedItemTitleAndDescription( - itemID: itemID, - objectID: linkedItem.objectID, - title: title, - author: author, - description: description - ) } + + // Send update to server + syncLinkedItemTitleAndDescription( + itemID: itemID, + title: title, + author: author, + description: description + ) } func syncLinkedItemTitleAndDescription( itemID: String, - objectID: NSManagedObjectID, title: String, author: String?, description: String @@ -64,7 +63,7 @@ extension DataService { let syncStatus: ServerSyncStatus = data == nil ? .needsUpdate : .isNSync context.perform { - guard let linkedItem = context.object(with: objectID) as? LinkedItem else { return } + guard let linkedItem = LinkedItem.lookup(byID: itemID, inContext: context) else { return } linkedItem.serverSyncStatus = Int64(syncStatus.rawValue) do { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index 1c9208240..7c2b94693 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -147,13 +147,12 @@ public extension DataService { break case .needsDeletion: item.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) - syncLinkDeletion(itemID: item.unwrappedID, objectID: item.objectID) + syncLinkDeletion(itemID: item.unwrappedID) case .needsUpdate: item.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) - syncLinkArchiveStatus(itemID: item.unwrappedID, objectID: item.objectID, archived: item.isArchived) + syncLinkArchiveStatus(itemID: item.unwrappedID, archived: item.isArchived) syncLinkReadingProgress( itemID: item.unwrappedID, - objectID: item.objectID, readingProgress: item.readingProgress, anchorIndex: Int(item.readingProgressAnchor) ) @@ -180,11 +179,7 @@ public extension DataService { case .needsUpdate: if let annotation = highlight.annotation { highlight.serverSyncStatus = Int64(ServerSyncStatus.isSyncing.rawValue) - syncHighlightAttributes( - highlightID: highlight.unwrappedID, - objectID: highlight.objectID, - annotation: annotation - ) + syncHighlightAttributes(highlightID: highlight.unwrappedID, annotation: annotation) } else { highlight.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue) }