Multiple Warnings when running Unit Tests in Sample app

Im following Chapter 7 of Core Data by Tutorials. I have run the test suite for the project in the final folder of the source code. There are multiple warnings getting logged when I run the tests:

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036c06e0) from NSManagedObjectModel (0x6000022f20d0) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036c06e0) from NSManagedObjectModel (0x6000022f20d0) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036c8370) from NSManagedObjectModel (0x6000022bfa70) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036c8370) from NSManagedObjectModel (0x6000022bfa70) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036c8370) from NSManagedObjectModel (0x6000022bfa70) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036c8370) from NSManagedObjectModel (0x6000022bfa70) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d4000) from NSManagedObjectModel (0x6000022f81e0) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d4000) from NSManagedObjectModel (0x6000022f81e0) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d4000) from NSManagedObjectModel (0x6000022f81e0) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d4000) from NSManagedObjectModel (0x6000022f81e0) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d4210) from NSManagedObjectModel (0x6000022f89b0) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d4210) from NSManagedObjectModel (0x6000022f89b0) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**


** CampgroundManager[1904:4899640] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899640] [error] warning:   'CampSite' (0x6000036d4210) from NSManagedObjectModel (0x6000022f89b0) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d4210) from NSManagedObjectModel (0x6000022f89b0) claims 'CampSite'.**

** CampgroundManager[1904:4899640] [error] warning:   'CampSite' (0x6000036c4580) from NSManagedObjectModel (0x600002247a70) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036c4580) from NSManagedObjectModel (0x600002247a70) claims 'CampSite'.**


** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036c08f0) from NSManagedObjectModel (0x6000022f20d0) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c08f0) from NSManagedObjectModel (0x6000022f20d0) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036c4b00) from NSManagedObjectModel (0x600002247a70) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c4b00) from NSManagedObjectModel (0x600002247a70) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036c4bb0) from NSManagedObjectModel (0x600002247de0) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c4bb0) from NSManagedObjectModel (0x600002247de0) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899655] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899655] [error] warning:   'Camper' (0x6000036c4bb0) from NSManagedObjectModel (0x600002247de0) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c4bb0) from NSManagedObjectModel (0x600002247de0) claims 'Camper'.**

**CampgroundManager[1904:4899655] [error] warning:   'Camper' (0x6000036c8000) from NSManagedObjectModel (0x60000225cc30) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c8000) from NSManagedObjectModel (0x60000225cc30) claims 'Camper'.**


** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036c8000) from NSManagedObjectModel (0x60000225cc30) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036c8000) from NSManagedObjectModel (0x60000225cc30) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036d00b0) from NSManagedObjectModel (0x6000022c2440) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036d00b0) from NSManagedObjectModel (0x6000022c2440) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036c8630) from NSManagedObjectModel (0x60000225cc30) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036c8630) from NSManagedObjectModel (0x60000225cc30) claims 'CampSite'.**

**CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d0000) from NSManagedObjectModel (0x6000022c2440) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d0000) from NSManagedObjectModel (0x6000022c2440) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Reservation' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Reservation' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'Reservation' (0x6000036c09a0) from NSManagedObjectModel (0x6000022f20d0) claims 'Reservation'.**

**CoreData: warning:   'Reservation' (0x6000036c09a0) from NSManagedObjectModel (0x6000022f20d0) claims 'Reservation'.**

** CampgroundManager[1904:4899594] [error] warning:   'Reservation' (0x6000036c8160) from NSManagedObjectModel (0x60000225cc30) claims 'Reservation'.**

**CoreData: warning:   'Reservation' (0x6000036c8160) from NSManagedObjectModel (0x60000225cc30) claims 'Reservation'.**

** CampgroundManager[1904:4899594] [error] warning:   'Reservation' (0x6000036d0160) from NSManagedObjectModel (0x6000022c2440) claims 'Reservation'.**

**CoreData: warning:   'Reservation' (0x6000036d0160) from NSManagedObjectModel (0x6000022c2440) claims 'Reservation'.**

** CampgroundManager[1904:4899594] [error] error: +[Reservation entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[Reservation entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**


** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Camper' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036d00b0) from NSManagedObjectModel (0x6000022c2440) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036d00b0) from NSManagedObjectModel (0x6000022c2440) claims 'Camper'.**

**CampgroundManager[1904:4899594] [error] warning:   'Camper' (0x6000036d82c0) from NSManagedObjectModel (0x600002298e10) claims 'Camper'.**

**CoreData: warning:   'Camper' (0x6000036d82c0) from NSManagedObjectModel (0x600002298e10) claims 'Camper'.**

** CampgroundManager[1904:4899594] [error] error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[Camper entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CampSite' so +entity is unable to disambiguate.**

**CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d0000) from NSManagedObjectModel (0x6000022c2440) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d0000) from NSManagedObjectModel (0x6000022c2440) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] warning:   'CampSite' (0x6000036d8370) from NSManagedObjectModel (0x600002298e10) claims 'CampSite'.**

**CoreData: warning:   'CampSite' (0x6000036d8370) from NSManagedObjectModel (0x600002298e10) claims 'CampSite'.**

** CampgroundManager[1904:4899594] [error] error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[CampSite entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

** CampgroundManager[1904:4899594] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Reservation' so +entity is unable to disambiguate.**

**CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Reservation' so +entity is unable to disambiguate.**

** CampgroundManager[1904:4899594] [error] warning:   'Reservation' (0x6000036d0160) from NSManagedObjectModel (0x6000022c2440) claims 'Reservation'.**

**CoreData: warning:   'Reservation' (0x6000036d0160) from NSManagedObjectModel (0x6000022c2440) claims 'Reservation'.**

** CampgroundManager[1904:4899594] [error] warning:   'Reservation' (0x6000036d8160) from NSManagedObjectModel (0x600002298e10) claims 'Reservation'.**

**CoreData: warning:   'Reservation' (0x6000036d8160) from NSManagedObjectModel (0x600002298e10) claims 'Reservation'.**

** CampgroundManager[1904:4899594] [error] error: +[Reservation entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

**CoreData: error: +[Reservation entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass**

Any advice on how to fix these warnings?

@astralbodies Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

This sounds like CodeGen is turned on for the 3 entities in the Core Data Model. That will cause it to create a hidden class for each entity. Since you have already defined a class for each entity, those generated ones will be duplicates.

CodeGen for each entity should be set to Manual/None in the data model. You may also need to do Clean Build Folder after changing them.

16%20AM

1 Like

Thanks for the response @sgerrard . Entities Camper, Campsite and Reservation have Manual/None for CodeGen already unfortunately. I also did a Clean Build Folder but the warnings still remain.

@gcpam Did you modify any of the code in the final folder before running the tests? What version of Xcode are you using?

@astralbodies Nope I didn’t modify any of the code. I was following the tutorial for unit testing and noticed the issues which prompted me to download the final project to check if these type of warnings were being logged, which they are. I’m using Xcode Version 10.2.1.

@gcpam Awesome, I’ll give it a look. It’s possible something snuck in between the last time we updated the book with Xcode. I’m also in the process of updating my chapters for Xcode 11/iOS 13 and will address any of the warnings there if they happen as well. I’ll let you know if there’s a quick fix :slight_smile:

1 Like

I had a little time to mess around with this. At first I thought I wasn’t getting the warnings, but when I checked, I was. Final Project, untouched, Xcode 10.2.1.

StackOverflow has some stuff on this. The warning occurs because two complete core data stacks are being created: the test stack, but also the regular stack. application(didFinishLaunchingWithOptions:) gets called, and the regular CoreDataStack gets created and passed around to the view controllers, even though they don’t get shown. There is probably some way to suppress that using test scheme options or something, but the simplest way is detailed at this GitHub link:

When you use the convenience init for your managed objects, it searches all the managed object models, and gives a warning if it finds two occurrences of your entity name. I guess that is okay, since I didn’t know the regular CoreDataStack was also loading, and it is good be alerted to it. It then uses the definition associated with the passed in context, so it works out fine and the tests succeed.

You can avoid the warnings by using a more explicit init, which is easiest to do if you add a new convenience init to NSManagedObject:

import CoreData

public extension NSManagedObject {
  convenience init(using usedContext: NSManagedObjectContext) {
    let name = String(describing: type(of: self))
    let entity = NSEntityDescription.entity(forEntityName: name, in: usedContext)!
    self.init(entity: entity, insertInto: usedContext)
  }
}

This makes the entity description specific to the passed in context, so there is no ambiguity, and therefore no more warning messages.

The corresponding code changes are in the CamperService, CampSiteService, and ReservationService, in the lines creating the Camper, CampSite, or Reservation respectively:

    let camper = Camper(using: managedObjectContext)
///
    let campSite = CampSite(using: managedObjectContext)
///
    let reservation = Reservation(using: managedObjectContext)

I’ve been digging into this more and it looks like there’s an issue with how the model is being loaded. I swapped out the AppDelegate for unit tests to prevent the full app from loading - the first test runs fine but subsequent tests after it give the warning/errors. I think NSPersistentContainer is somehow caching the models so that even if the container is de-initialized the previous model is sticking around. I’m going to dig into this further. In practice I don’t use NSPersistentContainer in production code only because the old way worked fine. :wink:

So it looks like the NSManagedObjectModel that’s created in the previous test’s run is cached by something - I can’t really figure out what or where (I’m suspecting some internal thing inside of NSPersistentContainer). I ended up getting around it by creating a static constant on CoreDataStack for the model so both the main app and the unit test stacks use the same model instance. It’s a hack, in my mind, because the persistent container should be releasing all instances of objects related to that container. I’ll update the code for the next version of the book which is due to come out in September.

Thanks so much for bringing this one to my attention @gcpam! It must have gotten missed when I converted to NSPersistentContainer for iOS 10.

No problem at all @astralbodies ! Thanks for taking the time looking into this!

This topic was automatically closed after 166 days. New replies are no longer allowed.