相关疑难解决方法(0)

将数据迁移到应用程序组会禁用 iCloud 同步

我正在向我现有的应用程序添加 Today Extension。我添加了一个应用程序组并使用这篇文章成功地将我的核心数据的数据迁移到了应用程序组的商店。我的应用程序同时使用 NSPersistentCloudKitContainer(当 iCloud 开启时)和一个 NSPersistentContainer(iCloud 关闭时)。虽然两个容器中的数据都成功迁移,但在使用 NSPersistentCloudKitContainer 时,我无法再在我的设备之间同步。在控制台中,我收到以下两个错误:

CoreData: 错误: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(837): : 无法为商店设置 CloudKit 集成: (URL: file://path/name.sqlite)

错误域=NSCocoaErrorDomain 代码=134060 “发生核心数据错误。” UserInfo={NSLocalizedFailureReason=镜像委托无法初始化,因为它的存储已从协调器中删除。}

第一条错误消息的路径是切换到 App Groups 之前的 oldURL 的路径。所以我相信我只需要告诉 iCloud 不要尝试在该商店位置集成 CloudKit 并使用应用程序组的商店位置。

但我不知道如何做到这一点。任何人都可以帮忙吗?

核心数据代码:

class CoreDataManager {
    static let sharedManager = CoreDataManager()
    private init() {}

    lazy var persistentContainer: NSPersistentContainer = {
        var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")

        //Get the correct container
        let containerToUse: NSPersistentContainer?
        if useCloudSync {
           containerToUse = NSPersistentCloudKitContainer(name: "App")
        } else {
            containerToUse …
Run Code Online (Sandbox Code Playgroud)

xcode core-data swift ios-app-group nspersistentcloudkitcontainer

12
推荐指数
1
解决办法
1087
查看次数

从 iOS 14 小部件的 CoreData 获取数据

我想在小部件中显示从 Core Data 获取的数据。但@FetchRequest不适用于小部件。

据我了解,我们必须创建一个应用程序组并创建一个共享的持久容器。

我想知道的是如何从共享的持久容器中读取(获取)小部件上的数据,或者简单地说,如何在小部件中显示从 Core Data 获取的数据

core-data swift widgetkit swiftui ios14

9
推荐指数
1
解决办法
4175
查看次数

在 NSPersistentStoreCoordinator 上调用 destroyPersistentStore 后,是否应该删除底层持久存储文件?

我正在将我的 iOS 应用程序迁移到使用NSPersistentContainer. 默认情况下,此类将其持久存储文件定位在Library/Application Support目录中;以前我的商店文件存储在该Documents目录中。

我添加了一些代码来移动存储文件(如果在旧目录中找到它们):

func moveStoreFromLegacyLocationIfNecessary(toNewLocation newLocation: URL) {

    // The old store location is in the Documents directory
    let legacyStoreLocation = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("books.sqlite")

    // Check whether the old store exists and the new one does not
    if FileManager.default.fileExists(atPath: legacyStoreLocation.path) && !FileManager.default.fileExists(atPath: newLocation.path) {

        print("Store located in Documents directory; migrating to Application Support directory")
        let tempStoreCoordinator = NSPersistentStoreCoordinator()
        try! tempStoreCoordinator.replacePersistentStore(at: newLocation, destinationOptions: nil, withPersistentStoreFrom: legacyStoreLocation, …
Run Code Online (Sandbox Code Playgroud)

sqlite core-data ios nspersistentstore swift

6
推荐指数
1
解决办法
592
查看次数

如何将Core Data的数据迁移到App Group的数据

我刚刚App Groups使用此StackOverFlow 帖子添加到我的应用程序中。不幸的是,由于defaultDirectoryURL正在发生变化,我无法获取在使用应用程序组目录之前制作的任何旧数据。我知道数据仍然存在,因为当我切换回使用常规NSPersistentContainer而不是 时GroupedPersistentContainer,我可以获取数据。

如何将旧数据迁移到我正在获取应用组数据的位置?

核心数据代码:

class CoreDataManager {
    static let sharedManager = CoreDataManager()
    private init() {}

    lazy var persistentContainer: NSPersistentContainer = {
        var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")

        let containerToUse: NSPersistentContainer?
        if useCloudSync {
           containerToUse = NSPersistentCloudKitContainer(name: "App")
        } else {
            containerToUse = GroupedPersistentContainer(name: "App")
            let description = containerToUse!.persistentStoreDescriptions.first
            description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
      }

        guard let container = containerToUse, let description = container.persistentStoreDescriptions.first else {
            fatalError("Hey Listen! ###\(#function): Failed …
Run Code Online (Sandbox Code Playgroud)

migration core-data swift ios-app-group nspersistentcontainer

5
推荐指数
0
解决办法
1673
查看次数

SecurityApplicationGroupIdentifier 的应用程序组返回 nil

我设置了一个应用程序组与我的 Today Extension 一起使用。我在主应用程序的目标和扩展程序的目标中添加了应用程序组。在开发人员门户的应用程序 ID 配置中,我启用了应用程序组。但由于某种原因,FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: id)每当我调用它时都会返回零。我已经检查了三次该forSecurityApplicationGroupIdentifier字符串并知道它是正确的。

有谁知道为什么这不起作用?

编辑:我能够通过编辑我的调试权利以包含应用程序组来解决零问题。我一直在关注这篇文章,并且能够成功地从 NSPersistentContainer 迁移我的数据,但是当我尝试在用户打开 iCloud 时使用 NSPersistentCloudKitContainer 时,同样的方法不起作用。它仍然能够迁移数据,但不允许我在设备之间同步数据(几乎只是使其成为常规的 NSPersistentContainer)。如果我恢复到以前的方式,我就可以使用 iCloud 同步。

NSPersistentCloudKitContainer任何人都可以帮助我解决迁移时使用应用程序组时的同步问题吗?

核心数据代码:

class CoreDataManager {
    static let sharedManager = CoreDataManager()
    private init() {}

    lazy var persistentContainer: NSPersistentContainer = {
        var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")

        //Get the correct container
        let containerToUse: NSPersistentContainer?
        if useCloudSync {
           containerToUse = NSPersistentCloudKitContainer(name: "App")
        } else {
            containerToUse = NSPersistentContainer(name: "App")      
        }

        guard let container = containerToUse else {
            fatalError("Couldn't get …
Run Code Online (Sandbox Code Playgroud)

xcode ios swift ios-app-group

4
推荐指数
1
解决办法
3767
查看次数