I am seeing a crash in core data that only happens after returning from the background. It works fine until I put the app in the background and return to foreground
The error is
[error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CDChatMessage' so +entity is unable to disambiguate. CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CDChatMessage' so +entity is unable to disambiguate. [error] warning: 'CDChatMessage' (0x2810a8a50) from NSManagedObjectModel (0x280485bd0) claims 'CDChatMessage'.
I have a singleton which handles the coredata functionality, My initialisation code for the CoreData in that singleton is:
override init() { super.init() let bundle = Bundle(for: CoreDataStack.self) guard let url = bundle.url(forResource: "Testing", withExtension: "momd"), let model = NSManagedObjectModel(contentsOf: url) else { fatalError() } self.storeCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model) self.context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) self.context?.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType) self.context?.persistentStoreCoordinator = self.storeCoordinator let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let docURL = urls[urls.endIndex-1] let storeURL = docURL.appendingPathComponent(kDatabaseName) do { try storeCoordinator?.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil) } catch { fatalError("Error migrating store: \(error)") } }
And the place where it works until I enter the background and then foreground is:
public func loadManaged<T: NSManagedObject>(forPredicate predicate: NSPredicate) -> [T] { let request: NSFetchRequest<T> = T.fetchRequest() as! NSFetchRequest<T> request.entity = T.entity() request.predicate = predicate do { return try context?.fetch(request) ?? [] } catch { return [T]() } }
(It crashes on the context?.fetch call)
What happens when it enters the background/foreground that causes the multiple NSEntityDescriptions? And am I doing something wrong in the code above, or is there something I can do differently to fix this crash?
https://stackoverflow.com/questions/66947669/ios-coredata-after-returning-from-background-crashes-with-coredata-warning-m April 05, 2021 at 10:39AM
没有评论:
发表评论