perform sync within conext perform blocks

This commit is contained in:
Satindar Dhillon
2023-02-06 13:06:25 -08:00
parent 4c73f79286
commit d00089ea25
5 changed files with 37 additions and 42 deletions

View File

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

View File

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

View File

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

View File

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

View File

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