Explorar el Código

Move send to perform block to avoid potential race

Marvin Polscheit hace 3 días
padre
commit
b54d2cadd7
Se han modificado 1 ficheros con 7 adiciones y 6 borrados
  1. 7 6
      Model/CoreDataStack.swift

+ 7 - 6
Model/CoreDataStack.swift

@@ -168,18 +168,19 @@ class CoreDataStack: ObservableObject {
         // Update view context with objectIDs from history change request
         /// - Tag: mergeChanges
         let viewContext = persistentContainer.viewContext
+        let changedObjectIDs = Set(history.flatMap { $0.changes ?? [] }.map(\.changedObjectID))
+
         viewContext.perform {
             for transaction in history {
                 viewContext.mergeChanges(fromContextDidSave: transaction.objectIDNotification())
                 self.lastToken = transaction.token
             }
-        }
 
-        // Notify app-side observers (services) about which objects changed. This history-sourced
-        // change feed replaces the hand-rolled changedObjectsOnManagedObjectContextDidSavePublisher.
-        let changedObjectIDs = Set(history.flatMap { $0.changes ?? [] }.map(\.changedObjectID))
-        if !changedObjectIDs.isEmpty {
-            entityChangeSubject.send(changedObjectIDs)
+            // Notify app-side observers (services) about which objects changed. This history-sourced
+            // change feed replaces the hand-rolled changedObjectsOnManagedObjectContextDidSavePublisher.
+            if !changedObjectIDs.isEmpty {
+                self.entityChangeSubject.send(changedObjectIDs)
+            }
         }
     }