如何解决如何选择性地将 iCloud 与核心数据一起使用?
我打算制作一个切换按钮,以便用户可以选择是否要与 iCloud 同步本地数据, 为用户提供更多隐私。
但我似乎只能选择使用 iCloud(NSPersistentCloudKit) 或不使用 (NSPersistentKit)
我找不到提供禁用功能的 NSPersistentCloudkit,或者 如果数据之前存储在本地设备中,是否可以通过 NSPersistentCloudkit 中的某些设置同步到云端?
还是像 WWDC19 所说的那样,如果需要数千行代码?
解决方法
我自己从未尝试过,但如果您使用自定义设置而不是标准设置,您可能会通过将 CloudKit 存储描述设为可选来实现。
lazy var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "Earthquakes")
// Create a store description for a local store
let localStoreLocation = URL(fileURLWithPath: "/path/to/local.store")
let localStoreDescription =
NSPersistentStoreDescription(url: localStoreLocation)
localStoreDescription.configuration = "Local"
//Make this section optional
// Create a store description for a CloudKit-backed local store
let cloudStoreLocation = URL(fileURLWithPath: "/path/to/cloud.store")
let cloudStoreDescription =
NSPersistentStoreDescription(url: cloudStoreLocation)
cloudStoreDescription.configuration = "Cloud"
//Make this section optional
// Set the container options on the cloud store
cloudStoreDescription.cloudKitContainerOptions =
NSPersistentCloudKitContainerOptions(
containerIdentifier: "com.my.container")
// Update the container's list of store descriptions
container.persistentStoreDescriptions = [
cloudStoreDescription,//Make this line optional
localStoreDescription
]
// Load both stores
container.loadPersistentStores { storeDescription,error in
guard error == nil else {
fatalError("Could not load persistent stores. \(error!)")
}
}
return container
}()
我自己从来没有做过,我会检查商店对用户改变主意的反应。
,我找到了一些答案,还没有自己尝试。 但以防万一有类似问题的读者急用。
这似乎提供了整个解决方案(ios13): https://fluffy.es/toggle-icloud-sync-nspersistentcloudkitcontainer/
另请注意,一旦用户选择不同步,上面删除 iCloud 副本, 而https://developer.apple.com/forums/thread/118924建议不要那样做。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。