gpt4 book ai didi

swift - 在运行时切换 iCloud 同步

转载 作者:行者123 更新时间:2023-12-03 23:49:15 25 4
gpt4 key购买 nike

我有一个 SwiftUI 应用程序,用户可以在应用程序内购买一些高级功能。其中一项功能是通过更多设备进行 iCloud 同步。我正在使用 CoreData 来保存用户数据。
我的持久容器:

lazy var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "store name")
let description: NSPersistentStoreDescription? = container.persistentStoreDescriptions.first
let remoteChangeKey: String = "NSPersistentStoreRemoteChangeNotificationOptionKey"
if(description != nil) {
description!.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description!.setOption(true as NSNumber, forKey: remoteChangeKey)
}

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

我的问题是如何在用户购买订阅时打开/关闭云同步。我不希望用户必须重新启动应用程序。
我还希望用户可以在应用内设置中切换此设置。

谢谢

最佳答案

将您的变量声明为 NSPersistentContainer而不是 NSPersistentCloudKitContainer .在启动时,如果用户有云同步,则加载云套件持久容器,否则加载非云套件一。

切换开关后,按照相同的规则重新加载容器。要重新加载容器,我会将属性添加到管理器对象,在其中我将添加一些根据用户设置重新加载容器的方法。

关于swift - 在运行时切换 iCloud 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60063102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com