How do I fix 'Multiple NSEntityDescriptions claim the NSManagedObject subclass'?

122 Views Asked by At

I know I'm not alone because I've looked at previous questions and answers about this problem but none of them have helped me solve my issue.

I'm getting these errors in the console when I run my unit tests:

Test Suite 'Selected tests' started at 2023-07-12 07:40:26.855
Test Suite 'My 1 Rep Max Tests.xctest' started at 2023-07-12 07:40:26.856
Test Suite 'DataControllerTests' started at 2023-07-12 07:40:26.856
Test Case '-[My_1_Rep_Max_Tests.DataControllerTests test_fetchLiftNames]' started.
container is about to be initialized and inMemory is true
Here's a datacontroller from setUpWithError <My_1_Rep_Max.DataController: 0x60000296c000>
Entity: Formula, Class: My_1_Rep_Max.Formula
Entity: Lift, Class: My_1_Rep_Max.Lift
Entity: LiftEvent, Class: My_1_Rep_Max.LiftEvent
Test Case '-[My_1_Rep_Max_Tests.DataControllerTests test_fetchLiftNames]' passed (18.144 seconds).
Test Case '-[My_1_Rep_Max_Tests.DataControllerTests test_newLiftEvent]' started.
container is about to be initialized and inMemory is true
Here's a datacontroller from setUpWithError <My_1_Rep_Max.DataController: 0x60000296c000>
2023-07-12 08:00:34.131784-0700 My 1 Rep Max[3038:55798] [error] **warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'My_1_Rep_Max.LiftEvent' so +entity is unable to disambiguate.
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'My_1_Rep_Max.LiftEvent' so +entity is unable to disambiguate.
2023-07-12 08:00:34.132390-0700 My 1 Rep Max[3038:55798] [error] warning:    'LiftEvent' (0x600003d3c160) from NSManagedObjectModel (0x600002960280) claims 'My_1_Rep_Max.LiftEvent'.
CoreData: warning:       'LiftEvent' (0x600003d3c160) from NSManagedObjectModel (0x600002960280) claims 'My_1_Rep_Max.LiftEvent'.
2023-07-12 08:00:34.132530-0700 My 1 Rep Max[3038:55798] [error] warning:    'LiftEvent' (0x600003d04160) from NSManagedObjectModel (0x600002974280) claims 'My_1_Rep_Max.LiftEvent'.
CoreData: warning:       'LiftEvent' (0x600003d04160) from NSManagedObjectModel (0x600002974280) claims 'My_1_Rep_Max.LiftEvent'.
2023-07-12 08:00:34.132656-0700 My 1 Rep Max[3038:55798] [error] error: +[My_1_Rep_Max.LiftEvent entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[My_1_Rep_Max.LiftEvent entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**
Entity: Formula, Class: My_1_Rep_Max.Formula
Entity: Lift, Class: My_1_Rep_Max.Lift
Entity: LiftEvent, Class: My_1_Rep_Max.LiftEvent
Test Case '-[My_1_Rep_Max_Tests.DataControllerTests test_newLiftEvent]' passed (1189.141 seconds).
Test Suite 'DataControllerTests' passed at 2023-07-12 08:00:34.141.
     Executed 2 tests, with 0 failures (0 unexpected) in 1207.285 (1207.286) seconds
Test Suite 'My 1 Rep Max Tests.xctest' passed at 2023-07-12 08:00:34.142.
     Executed 2 tests, with 0 failures (0 unexpected) in 1207.285 (1207.286) seconds
Test Suite 'Selected tests' passed at 2023-07-12 08:00:34.142.
     Executed 2 tests, with 0 failures (0 unexpected) in 1207.285 (1207.287) seconds

I have a BaseTestCase class for my DataContollers tests:

class BaseTestCase: XCTestCase {
var dataController: DataController!
var managedObjectContext: NSManagedObjectContext!

override func setUpWithError() throws {
    dataController = DataController(inMemory: true)
    managedObjectContext = dataController.container.viewContext
   }
 }

and this DataControllerTests class which causes the errors to appear:

class DataControllerTests: BaseTestCase {

    override func setUp() {
        super.setUp()

    }

    override func tearDown() {
        dataController = nil
        super.tearDown()
    }
    
    func test_newLiftEvent() {

        let liftEvent = LiftEvent(context: managedObjectContext)
        liftEvent.setValue(200.0, forKey: "weightLifted")
        liftEvent.setValue(3, forKey: "repetitions")

        XCTAssertNotNil(liftEvent)
    }

    func test_fetchLiftNames() {

        dataController.createSampleLiftData()

        let fetchedLifts = dataController.fetchLiftNames()

        XCTAssertEqual(fetchedLifts.count, 13, "Expected 13 standard lift names")
    }

}

You can see in the console messages that I've got 2 different managedObjectModels claiming 'LiftEvent' managed objects. It's only in the test_newLiftEvent() test that CoreData starts complaining.

I thought I was getting a good understanding of CoreData until I ran into this. How can I fix this?

Thanks!

Update 1: I set some breakpoints and discovered something I didn't expect. The func test_fetchLiftNames() test runs first and func setUpWithError() in BaseTestCase is called creating an instance of DataController. The test passes with no CoreDataErrors.

Next the func test_newLiftEvent() test runs and BaseTestCase is called again and a new DataController instance is created when func setUpWithError() is called. Then when the test actually runs, I get the CoreData errors.

This doesn't make sense to me. Any idea why it's called for each test?

0

There are 0 best solutions below