简体   繁体   中英

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.

Now we re-written the full application in Swift and this App is using 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

During fetch following error was thrown.

Could not fetch. 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. 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. To migrate it, you need to reference the location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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