【问题标题】:Toggle iCloud sync during runtime在运行时切换 iCloud 同步
【发布时间】:2023-03-29 09:18:01
【问题描述】:

我有一个 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
    }()

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

谢谢

【问题讨论】:

    标签: swift xcode icloud nspersistentcloudkitcontainer


    【解决方案1】:

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

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

    【讨论】:

    • 类似的东西? `lazy var ..... let container = self.iapManager.isPremiumVersion ? NSPersistentCloudKitContainer(name: "store name") : NSPersistentContainer(name: "store name") .... ` 但是我可以重新加载容器吗?
    • 我已经找到了解决办法:stackoverflow.com/questions/65355720/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多