23

When I use my app, sometimes, I have an error that appear, it seems to be randomly (or I didn't figure out when exactly...), then all my lists are empty (like if nothing were in CoreData). But If I close my app and re-open it, lists appear without any problem...

I searched on stack overflow about this problem, but nothing is clear for me...

Error :

CoreData: warning:       'CDDetail' (0x2815e8790) from NSManagedObjectModel (0x2841bb8e0) claims 'CDDetail'.
2020-11-13 19:16:48.329773+0100 OrientationEPS[33705:3479327] [error] error: +[CDDetail entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[CDDetail entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass

Loading the persistent Container :

class OriEPS {

lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "OrientationEPS")
        container.loadPersistentStores { description, error in
            if let error = error {
                 fatalError("Unable to load persistent stores: \(error)")
            }
        }
        return container
    }()

var context: NSManagedObjectContext {
    return persistentContainer.viewContext
}

And Here is my function to fetch the result :

private func fetchCDDetail(withId detailId:UUID) -> CDDetail? {
    let fetchRequest = NSFetchRequest<CDDetail>(entityName: "CDDetail")
    fetchRequest.predicate = NSPredicate(format: "id == %@", detailId as CVarArg)
    fetchRequest.fetchLimit = 1
    let fetchResult:[CDDetail]? = try? context.fetch(fetchRequest)
    return fetchResult?.first
}

My CD Model CDModel

2 questions : How should I solve this error ? What does mean 0x2815e8790 ?

Edit 1 : I can't find any other class call CDDetail

  1. If I set Module to Current Product Module (nothing change)
  2. Nothing change If I replace :
  • fetchRequest:NSFetchRequest = CDDetail.fetchRequest()

by

  • fetchRequest = NSFetchRequest(entityName:"CDDetail")
  1. If I set codeGen to Category/Extension, it does not build and give me lots of errors : erros
Clément Tengip
  • 618
  • 6
  • 19
  • Do you have more than one class named `CDDetail`? That's what the error message sounds like. That might happen if you made your own class but also left automatic class generation on in the Core Data model editor. – Tom Harrington Nov 13 '20 at 20:48
  • 1
    I updated my question by adding a screenshot of my CoreData Model. I only have one entity CDDetail. – Clément Tengip Nov 13 '20 at 21:12
  • OK that shows you have automatic class generation turned on. Do you have a second version of the class that you created? That’s what I’m getting at, because the error message sounds like there are two conflicting class definitions. – Tom Harrington Nov 14 '20 at 23:16
  • 1
    What do you mean about a second version ? – Clément Tengip Nov 15 '20 at 08:29
  • It looks like something bad with project. Can you give access somehow to entire project? – Asperi Nov 16 '20 at 16:35
  • What I meant was did you create a class named `CDDetail` yourself, in addition to the one created by Xcode, so that there are two classes with the same name. – Tom Harrington Nov 16 '20 at 17:18
  • No other class called CDDetail in my project – Clément Tengip Nov 16 '20 at 17:49
  • @ClémentTengip You have codegen set to class definition which means Xcode will generate the `CDDetail` in derrived data. But you have also created that class in project manually thus the error. Change the codegen to manual/none, remove derrived data and re-generate the `NSManagedObject` entities. Should work then – Lachtan Mar 10 '22 at 04:47

1 Answers1

23

You loaded model several times - that's the reason of those errors. Possible solution is to make container static.

Tested with Xcode 12.1 / iOS 14.1 - no errors:

class OriEPS {
    
    
   private static var persistentContainer: NSPersistentContainer = {
            let container = NSPersistentContainer(name: "CDOriEPS")
            container.loadPersistentStores { description, error in
                if let error = error {
                     fatalError("Unable to load persistent stores: \(error)")
                }
            }
            return container
        }()
    
    var context: NSManagedObjectContext {
        return Self.persistentContainer.viewContext
    }

    // ... other code

Note: other possible approach is to make OriEPS shared and use same instance everywhere you create it, but I did not investigate your solution deeply, so it is for your consideration.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690