My M1 Max with 64 Gb RAM just crashed because of this. Xcode 14 release, not beta, MacOS 12.6, latest update. Trying to compile this a SwiftUI view with a Table in it.
Post
Replies
Boosts
Views
Activity
OK, if anybody ever looks for a similar question (or myself 3 years from now), here is a simple answer. Don't load your model in PersistentController initializer as this is what loads the model multiple times. Just make it static (either a let constant or a singleton) and attach it to NSPersistentContainer. Here is an example/
let modelURL = Bundle.main.url(forResource: "MyModel", withExtension: "momd")!
let dataModel = NSManagedObjectModel(contentsOf: modelURL)!
struct PersistenceController {
let container: NSPersistentContainer
init(inMemory: Bool = false, name: String = "preview") {
container = NSPersistentContainer(name: name, managedObjectModel: dataModel)
// other stuff
}
}
Correction: the crash does NOT happen in container.loadPersistentStores call. This call completes successfully.
The crash happens when a new document is opened and the document view is being constructed.
After the first document is open:
2021-12-03T11:06:05-0800 debug: in view body, context: <NSManagedObjectContext: **0x60000348dad0>** , class: , hash: **105553171372752** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x6000021a8d00>)** , name: nil , objects: []
2021-12-03T11:06:05-0800 debug: in view onAppear, context: <NSManagedObjectContext: **0x60000348dad0>** , class: , hash: **105553171372752** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x6000021a8d00>)** , name: nil , objects: []
And then when I open a second document:
2021-12-03T11:08:02-0800 debug: in view body, context: <NSManagedObjectContext: **0x6000034a81a0>** , class: , hash: **105553171480992** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x600002198a80>)** , name: nil , objects: []
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'DBObject' so +entity is unable to disambiguate.
So the second view definitely gets a new context and a new coordinator, but CoreData for some reason gets confused with the same entity being loaded twice, even though they belong to two different contexts.
Any idea how to disambiguate the two contexts?
Correction: the crash does NOT happen in container.loadPersistentStores call. This call completes successfully. The crash happens when a new document is opened and the document view is being constructed.
After the first document is open:
2021-12-03T11:06:05-0800 debug: in view body, context: <NSManagedObjectContext: **0x60000348dad0>** , class: , hash: **105553171372752** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x6000021a8d00>)** , name: nil , objects: []
2021-12-03T11:06:05-0800 debug: in view onAppear, context: <NSManagedObjectContext: **0x60000348dad0>** , class: , hash: **105553171372752** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x6000021a8d00>)** , name: nil , objects: []
And then when I open a second document:
2021-12-03T11:08:02-0800 debug: in view body, context: <NSManagedObjectContext: **0x6000034a81a0>** , class: , hash: **105553171480992** , coordinator: Optional(<NSPersistentStoreCoordinator: **0x600002198a80>)** , name: nil , objects: []
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'DBObject' so +entity is unable to disambiguate.
So the second view definitely gets a new context and a new coordinator, but CoreData for some reason gets confused with the same entity being loaded twice, even though they belong to two different contexts.
In case anyone has the same question, I couldn't make the new TableView work with dynamic data source. Then I tried constructing a bunch of TextFields dynamically in a grid. It kind of worked, but it was very sluggish when scrolling. So I ended up wrapping NSTableView in NSViewRepresentable and working directly with NSTableView APIs. Performance is great with NSTableView and NSTextField as a cell view.
@Claude31, you mentioned that it's much easier in MacOS? Do you mind sharing more details on this, please?
Thanks!
I was hoping to use the new Table view in SwiftUI on Mac OS.