Я использую Swift5 и SwiftUI для создания приложения на основе CloudKit.
Я выполнил объединение данных из функции iCloud, и, похоже, оно работает хорошо. Но я всегда получаю предупреждение типа «Захват «objectIDNotifications» с неотправляемым типом «[Notification]» в закрытии «@Sendable». Мой код в основном относится к демо-версии Apple.
Мой код:
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")
}
}
}
}
Код Apple:
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())
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... ation-in-a