【发布时间】:2023-03-09 05:28:01
【问题描述】:
我正在使用 SwiftUI 开发 macOS 和 iOS 应用程序。两者都使用 CoreData 和 iCloudKit 在两个平台之间同步数据。它确实与同一个 iCloud 容器配合得很好。
我面临一个问题,即在应用程序中未触发 iCloud 后台更新。如果我在两个系统上都进行了更改,则会推送更改,但在另一台设备上不可见。
我需要重新加载应用程序、关闭应用程序并再次打开,或者在我的 Mac 应用程序中失去焦点并返回它。然后我的List 将被刷新。我不知道为什么它不工作,同时留在应用程序内而不会失去焦点。
我在 Stackoverflow 中阅读了几个线程,但是它们对我不起作用。这是我在 iOS 中的简单视图
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@State private var refreshing = false
private var didSave = NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave)
@FetchRequest(entity: Person.entity(), sortDescriptors: []) var persons : FetchedResults<Person>
var body: some View {
NavigationView
{
List()
{
ForEach(self.persons, id:\.self) { person in
Text(person.firstName + (self.refreshing ? "" : ""))
// here is the listener for published context event
.onReceive(self.didSave) { _ in
self.refreshing.toggle()
}
}
}
.navigationBarTitle(Text("Person"))
}
}
}
在此示例中,我已经使用了一种解决方法,Asperi 在另一个问题中进行了描述。但是,这对我也不起作用。列表未刷新。
在日志中,我可以看到它没有 ping iCloud 进行刷新。只有当我重新打开应用程序时。为什么后台模式不起作用?我已正确激活所有内容并设置了我的 AppDelegate。
lazy var persistentContainer: NSPersistentCloudKitContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
container.persistentStoreDescriptions.forEach { storeDesc in
storeDesc.shouldMigrateStoreAutomatically = true
storeDesc.shouldInferMappingModelAutomatically = true
}
//let container = NSPersistentCloudKitContainer(name: "NAME")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
UIApplication.shared.registerForRemoteNotifications()
return container
}()
编辑:
我的 iOS 应用仅在重新打开应用时才继续从 iCloud 获取记录。看这个 gif:
【问题讨论】:
-
一些副业 cmets...当您在问题中包含样板代码时,无需包含 Apple 包含的警告...还有
.shouldMigrateStoreAutomatically和.shouldInferMappingModelAutomatically都默认为 @987654328 @ 所以这些电话是必要的......最后,您无需致电UIApplication.shared.registerForRemoteNotifications(),因为您应该在“后台模式”下的“签名和功能”选项卡中检查这一点。 -
核心数据实体符合 ObservableObject 并且默认发布,所以通知对我来说似乎完全没有必要。也许我误解了你的需要?
标签: ios swift core-data swiftui icloud