我们的 Core Data + CloudKit 同步工作正常,但当我们卸载并重新安装我们的应用程序时出现问题。然后,我们要稍等一下,然后向 Core Data 发出获取请求。但我们不使用任何
DispatchQueue.main.asyncAfter(deadline: .now() + 8)
。有没有其他方法可以做到这一点或任何方法,比如当我们的数据从 iCloud 获取到 Core Data,然后我们在 SwiftUI 中请求数据到 Core Data 时他们通知我?
我尝试过
DispatchQueue.main.asyncAfter(deadline: .now() + 8)
。这种方式工作正常,但现在我们不想使用这种方式。我们如何处理这个问题?
您可以监控
NSPersistentCloudKitContainer.eventChangedNotification
.onReceive(NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification), perform: { notification in
guard let event = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey]
as? NSPersistentCloudKitContainer.Event else {
return
}
print("iCloud \(event.type)")
if event.endDate == nil {
print("\tis in Progress")
} else if let _ = event.endDate {
//Done
switch event.succeeded{
case true:
print("\tsucceeded")
case false:
print("\tfailed. 🛑")
if let error = event.error{
print("\t \(error)")
}
}
}
})