首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSQLite到Seam3存储类型的迁移在EXC_BAD_ACCESS中失败

NSSQLite到Seam3存储类型的迁移在EXC_BAD_ACCESS中失败
EN

Stack Overflow用户
提问于 2017-11-03 11:12:54
回答 1查看 117关注 0票数 0

我仍在尝试设置现有NSSQLiteStore到SMStore.Type (Seam3)的适当迁移,并遇到一些奇怪的问题。不明白为什么会发生这种事。我已经提出了保罗在github上也一样,但是可能有人会注意到我代码中的原因,并且能够在Paul忙的时候提供帮助。

我有什么:在iOS 10+上运行的应用程序,NSSQLiteStore位于默认文件夹中。

测试步骤:

  1. 删除新存储(如果存在的话),并且它的支持文件位于新路径: file:///Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Data/Application/29B60BBC-0D17-4D6C-8107-0135C45B20BA/Documents/CafeManagerSeam3.sqlite
  2. 运行应用程序。应用程序应该检查新商店是否存在。如果没有-执行从默认迁移。
  3. 应用程序试图迁移存储,但在EXC_BAD_ACCESS中失败了。但在同一时间,我可以看到新的商店出现在新的路径,如果我再次运行应用程序,它使用新的商店,没有明显的问题。

我的代码:

代码语言:javascript
复制
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?
    var smStore: SMStore?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 10.0, *) {
        let storeDescriptionType = AppDelegate.persistentContainer.persistentStoreCoordinator.persistentStores.first?.type
            if storeDescriptionType == SMStore.type {
                print("Store is SMStore")
                print()
                self.smStore = AppDelegate.persistentContainer.persistentStoreCoordinator.persistentStores.first as? SMStore
            }
        } else {
            let storeDescriptionType = AppDelegate.managedObjectContext.persistentStoreCoordinator?.persistentStores.first?.type
            if storeDescriptionType == SMStore.type {
                print("Store is SMStore")
                print()
                self.smStore = AppDelegate.managedObjectContext.persistentStoreCoordinator?.persistentStores.first as? SMStore
            }
        }
...
    // MARK: - Core Data stack for iOS 10+
    @available(iOS 10.0, *)
    static var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "CafeManager")
        let persistentStoreCoordinator = container.persistentStoreCoordinator

        //MARK: Initializing Seam3
        SMStore.registerStoreClass()
        SMStore.syncAutomatically = true

        //MARK: Preparing URL
        let applicationDocumentsDirectory: URL = {
            let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
            return urls[urls.count-1]
        }()
        let newURL = applicationDocumentsDirectory.appendingPathComponent("CafeManagerSeam3.sqlite")

        //MARK: Check if SQLite store has been already migrated by checking if CafeManagerSeam3.sqlite exists.
        let seamStoreExists = FileManager.default.fileExists(atPath: newURL.path)

        if seamStoreExists {
            //If exists, then use it because it has been already migrated to Seam3 storage
            print("Already migrated, using \(newURL)")

            let storeDescription = NSPersistentStoreDescription(url: newURL)
            storeDescription.type = SMStore.type
            storeDescription.setOption("iCloud.iGlock.CafeManager.com" as NSString, forKey: SMStore.SMStoreContainerOption)
            storeDescription.setOption(NSNumber(value:SMSyncConflictResolutionPolicy.clientTellsWhichWins.rawValue), forKey:SMStore.SMStoreSyncConflictResolutionPolicyOption)
            container.persistentStoreDescriptions=[storeDescription]

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

        } else {
            //If does not exist, then migrate old storage to Seam3.
            print("Not yet migrated, migrating to \(newURL)")

            //Loadig default store
            container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                if let error = error as NSError? {
                    fatalError("Failed to load default store \(error), \(error.userInfo)")
                }
            })
            let defaultPersistentStore = container.persistentStoreCoordinator.persistentStores.last
            print("Default store is located here: \(defaultPersistentStore!.url!)")

            //Adding new Seam3 store
            do {
                try persistentStoreCoordinator.addPersistentStore(ofType: SMStore.type, configurationName: nil, at: newURL, options: nil)
                print("Seam store was added to the new url: \(newURL)")
            } catch {
                fatalError("Failed to add new Seam store: \(error)")
            }

            //Migrating default store to new Seam store
            do {
                try persistentStoreCoordinator.migratePersistentStore(defaultPersistentStore!, to: newURL, options: nil, withType:SMStore.type)
                //Removing old store
                if defaultPersistentStore != nil {
                    do {
                        try persistentStoreCoordinator.remove(defaultPersistentStore!)
                    } catch {
                        fatalError("Failed to remove default store \(error)")
                    }
                }
            }
            catch {
                fatalError("Failed to migrate to Seam store: \(error)")
            }

            //Setting additional parameters to Seam store to make it able to use CloudKit
            let storeDescription = NSPersistentStoreDescription(url: newURL)
            storeDescription.type = SMStore.type
            storeDescription.setOption("iCloud.iGlock.CafeManager.com" as NSString, forKey: SMStore.SMStoreContainerOption)
            storeDescription.setOption(NSNumber(value:SMSyncConflictResolutionPolicy.clientTellsWhichWins.rawValue), forKey:SMStore.SMStoreSyncConflictResolutionPolicyOption)
            container.persistentStoreDescriptions=[storeDescription]

            return container
        }

输出:

代码语言:javascript
复制
CoreData: annotation:  Failed to load optimized model at path '/Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Bundle/Application/3E4EB624-227E-4BD0-99F1-AA975D86BDA8/CafeManager.app/CafeManager.momd/CafeManager v2.omo'
Not yet migrated, migrating to file:///Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Data/Application/100FB44C-C881-44C1-9D03-454FEBDB092B/Documents/CafeManagerSeam3.sqlite
Default store is located here: file:///Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Data/Application/100FB44C-C881-44C1-9D03-454FEBDB092B/Library/Application%20Support/CafeManager.sqlite
Seam store was added to the new url: file:///Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Data/Application/100FB44C-C881-44C1-9D03-454FEBDB092B/Documents/CafeManagerSeam3.sqlite
2017-10-31 17:49:47.762 CafeManager[52561:7472303] Access to CloudKit has not been verified by calling verifyCloudKitConnection
(lldb) po self
error: Trying to put the stack in unreadable memory at: 0x7ffee595ef80.
(lldb) 
What am I doing wrong? Is it my misunderstanding of CoreData stack or something else?

崩溃后重新运行应用程序时的输出:

代码语言:javascript
复制
objc[52621]: Class _NSZombie_OS_xpc_endpoint is implemented in both ?? (0x618000048340) and ?? (0x600000047290). One of the two will be used. Which one is undefined.
CoreData: annotation:  Failed to load optimized model at path '/Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Bundle/Application/0B098A31-C927-44C0-87C2-2F2B944DF66C/CafeManager.app/CafeManager.momd/CafeManager v2.omo'
Already migrated, using file:///Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Data/Application/932E6B3A-65B1-44F1-920B-DCE5F242C9C7/Documents/CafeManagerSeam3.sqlite
Store is SMStore

Sync Started
No more records coming
Sync Performed
Sync performed successfully
//some test insert that was loaded to Cloud.
Sync Started
No more records coming
Sync Performed
Sync performed successfully
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-13 18:40:26

错误解决了。用了保罗的示例

代码语言:javascript
复制
{
            // If does not exist, then migrate old storage to Seam3.
            print("Not yet migrated, migrating to \(newURL)")
            SMStore.syncAutomatically = false

            // Loadig default store
            container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                if let error = error as NSError? {
                    fatalError("Failed to load default store \(error), \(error.userInfo)")
                }
            })
            let defaultPersistentStore = container.persistentStoreCoordinator.persistentStores.last
            print("Default store is located here: \(defaultPersistentStore!.url!)")

            //Migrating default store to new Seam store
            do {
                try persistentStoreCoordinator.migratePersistentStore(defaultPersistentStore!, to: newURL, options: nil, withType:SMStore.type)
            }
            catch {
                fatalError("Failed to migrate to Seam store: \(error)")
            }

            //Setting additional parameters to Seam store to make it able to use CloudKit
            let storeDescription = NSPersistentStoreDescription(url: newURL)
            storeDescription.type = SMStore.type
            storeDescription.setOption("iCloud.iGlock.CafeManager.com" as NSString, forKey: SMStore.SMStoreContainerOption)
            storeDescription.setOption(NSNumber(value:SMSyncConflictResolutionPolicy.clientTellsWhichWins.rawValue), forKey:SMStore.SMStoreSyncConflictResolutionPolicyOption)
            container.persistentStoreDescriptions=[storeDescription]
            SMStore.syncAutomatically = true
            return container
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47094413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档