Skip to content

Commit

Permalink
Merge pull request #20280 from wordpress-mobile/update-perform-save-i…
Browse files Browse the repository at this point in the history
…n-notification-sync-mediator

Add another `performAndSave` overload
  • Loading branch information
crazytonyli authored Mar 9, 2023
2 parents 795e06a + b7fa576 commit 373c9a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
17 changes: 7 additions & 10 deletions WordPress/Classes/Services/NotificationSyncMediator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let NotificationSyncMediatorDidUpdateNotifications = "NotificationSyncMediatorDi
final class NotificationSyncMediator {
/// Returns the Main Managed Context
///
fileprivate let contextManager: CoreDataStack
private let contextManager: CoreDataStackSwift

/// Sync Service Remote
///
Expand Down Expand Up @@ -71,7 +71,7 @@ final class NotificationSyncMediator {
/// - manager: ContextManager Instance
/// - wordPressComRestApi: The WordPressComRestApi that should be used.
///
init?(manager: CoreDataStack, dotcomAPI: WordPressComRestApi) {
init?(manager: CoreDataStackSwift, dotcomAPI: WordPressComRestApi) {
guard dotcomAPI.hasCredentials() else {
return nil
}
Expand Down Expand Up @@ -311,7 +311,7 @@ private extension NotificationSyncMediator {
///
func determineUpdatedNotes(with remoteHashes: [RemoteNotification], completion: @escaping (([String]) -> Void)) {
Self.operationQueue.addOperation(AsyncBlockOperation { [contextManager] done in
contextManager.performAndSave { context in
contextManager.performAndSave({ context in
let remoteIds = remoteHashes.map { $0.notificationId }
let predicate = NSPredicate(format: "(notificationId IN %@)", remoteIds)
var localHashes = [String: String]()
Expand All @@ -320,19 +320,16 @@ private extension NotificationSyncMediator {
localHashes[note.notificationId] = note.notificationHash ?? ""
}

let outdatedIds = remoteHashes
return remoteHashes
.filter { remote in
let localHash = localHashes[remote.notificationId]
return localHash == nil || localHash != remote.notificationHash
}
.map { $0.notificationId }

DispatchQueue.main.async {
completion(outdatedIds)
}

}, completion: { outdatedIds in
completion(outdatedIds)
done()
}
}, on: .main)
})
}

Expand Down
23 changes: 23 additions & 0 deletions WordPress/Classes/Utility/ContextManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ let ContextManagerModelNameCurrent = "$CURRENT"

public protocol CoreDataStackSwift: CoreDataStack {

/// Execute the given block with a background context and save the changes.
///
/// This function _does not block_ its running thread. The block is executed in background and its return value
/// is passed onto the `completion` block which is executed on the given `queue`.
///
/// - Parameters:
/// - block: A closure which uses the given `NSManagedObjectContext` to make Core Data model changes.
/// - completion: A closure which is called with the return value of the `block`, after the changed made
/// by the `block` is saved.
/// - queue: A queue on which to execute the completion block.
func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) -> T, completion: ((T) -> Void)?, on queue: DispatchQueue)

/// Execute the given block with a background context and save the changes _if the block does not throw an error_.
///
/// This function _does not block_ its running thread. The block is executed in background and the return value
Expand Down Expand Up @@ -128,6 +140,17 @@ public class ContextManager: NSObject, CoreDataStack, CoreDataStackSwift {
})
}

public func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) -> T, completion: ((T) -> Void)?, on queue: DispatchQueue) {
performAndSave(
block,
completion: { (result: Result<T, Error>) in
// It's safe to force-unwrap here, since the `block` does not throw an error.
completion?(try! result.get())
},
on: queue
)
}

public func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) throws -> T) async throws -> T {
try await withCheckedThrowingContinuation { continuation in
performAndSave(block, completion: continuation.resume(with:), on: DispatchQueue.global())
Expand Down

0 comments on commit 373c9a1

Please sign in to comment.