Ich habe die Funktion zum Zusammenführen von Daten aus iCloud erstellt und sie scheint gut zu funktionieren. Aber ich erhalte immer eine Warnung wie „Erfassung von ‚objectIDNotifications‘ mit nicht sendbarem Typ ‚[Notification]‘ in einem ‚@Sendable‘-Abschluss“. Mein Code bezieht sich im Wesentlichen auf Apples Demo
Mein Code:
Code: Select all
extension AppState {
private func mergeRemoteChanges(_ notification: Notification) {
guard let transactions = notification.userInfo?[UserInfoKey.transactions] as? [NSPersistentHistoryTransaction] else {
return
}
let objectIDNotifications = transactions.compactMap { $0.objectIDNotification() }
let viewContext = PersistenceController.shared.persistentContainer.viewContext
viewContext.perform {
objectIDNotifications.forEach { changes in // warning here
viewContext.mergeChanges(fromContextDidSave: changes)
AppLog.data.trace("Merged remote changes")
}
}
}
}
Code: Select all
func photoTransactions(from notification: Notification) -> [NSPersistentHistoryTransaction] {
var results = [NSPersistentHistoryTransaction]()
if let transactions = notification.userInfo?[UserInfoKey.transactions] as? [NSPersistentHistoryTransaction] {
let photoEntityName = Photo.entity().name
for transaction in transactions where transaction.changes != nil {
for change in transaction.changes! where change.changedObjectID.entity.name == photoEntityName {
results.append(transaction)
break // Jump to the next transaction.
}
}
}
return results
}
func mergeTransactions(_ transactions: [NSPersistentHistoryTransaction], to context: NSManagedObjectContext) {
context.perform {
for transaction in transactions {
context.mergeChanges(fromContextDidSave: transaction.objectIDNotification())
}
}
}
Mobile version