Unit Test can't find Core Data model file

前端 未结 5 1345
無奈伤痛
無奈伤痛 2020-12-10 02:21

I\'ve created a project with a Core Data model in it. The application looks for the model file (.momd) and runs just fine.

Unfortunately, the unit test keeps returni

5条回答
  •  Happy的楠姐
    2020-12-10 02:56

    My problem was indeed the wrong Bundle! As I was trying to use a database from/within a Framework I 'simply' has to load the db from the corresponding Bundle !

    Here is some code in Swift4 using MagicalRecord:

    // Load the bundle
    let frameworkBundle = Bundle(for: AClassFromTheFramework.self)
    let managedObjectModel = NSManagedObjectModel.mergedModel(from: [frameworkBundle])
    // Use the new `managedObjectModel` by default
    MagicalRecord.setShouldAutoCreateManagedObjectModel(false)
    NSManagedObjectModel.mr_setDefaultManagedObjectModel(managedObjectModel)
    // Load the database 
    MagicalRecord.setupCoreDataStack(withAutoMigratingSqliteStoreNamed: "db.sqlite")
    

    And voilà!

提交回复
热议问题