AppleBee
AppleBee

Reputation: 1219

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?

Upvotes: 0

Views: 501

Answers (1)

Cristian Pena
Cristian Pena

Reputation: 2249

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.

Upvotes: 0

Related Questions