简体   繁体   English

使用Objective C创建并从Swift获取的核心数据

[英]Core data created with Objective C and fetch from Swift

I have an App which is using core data and written in Objective C.Here we are not using persistentContainer. 我有一个使用核心数据并用Objective C编写的应用程序。在这里,我们没有使用persistentContainer。

Now we re-written the full application in Swift and this App is using Realm. 现在,我们在Swift中重新编写了完整的应用程序,并且该应用程序正在使用Realm。

I want to migrate one DB entity from core data to realm and delete core data file after this. 我想将一个数据库实体从核心数据迁移到领域,然后删除核心数据文件。

lazy var persistentContainer: NSPersistentContainer = {

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

 var personData: [NSManagedObject] = []
            let managedContext = self.persistentContainer.viewContext

            let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
            do {
                offlineData = try managedContext.fetch(fetchRequest)
            } catch let error as NSError {
                print("Could not fetch. \(error), \(error.userInfo)")
            }


            if  (personData.count) > 0 { // Insert DB into Relam

            }

I am getting following errors 我遇到以下错误

error: Failed to load model named HitList CoreData: error: Failed to load model named HitList 错误:未能加载名为HitList的模型CoreData:错误:未能加载名为HitList的模型

During fetch following error was thrown. 在获取期间,引发了以下错误。

Could not fetch. 无法提取。 Error Domain=Foundation._GenericObjCError Code=0 "(null)", [:] Error Domain = Foundation._GenericObjCError Code = 0“(null)”,[:]

What is wrong? 怎么了?

The issue you are having is that the CoreData stack from Objective C saves the database to a different path than the persistentContainer. 您遇到的问题是,Objective C中的CoreData堆栈将数据库保存到与persistentContainer不同的路径。 Therefore, it won't find it because it's looking in a different location. 因此,找不到它是因为它在其他位置。 You can connect a device and check it with iExplorer and you'll see. 您可以连接设备并使用iExplorer对其进行检查,您会看到。 To migrate it, you need to reference the location. 要迁移它,您需要引用位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM