avoid using coredata cache in data service network calls handlers

This commit is contained in:
Satindar Dhillon 2023-02-06 10:46:16 -08:00
parent b47b6cba53
commit 4c73f79286
6 changed files with 50 additions and 64 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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