I have an experience with NSFetchedResultsController like this below is sample project that can manage 2 CoreData containers of 2 SQLite files with the same CoreData model.
https://www.dropbox.com/s/joj2n5jqmkkbson/test0002-2ContainerWork.zip?dl=0
However, I try to implement the same concept with SwiftUI like this below sample project.
https://www.dropbox.com/s/wn1lxwmafg3fp63/testCoreData3.zip?dl=0
The important point, when I run only 1 view. This project can run correctly for each CoreData container. But when I run both views, the Xcode shows error !!!
Here is code of TabView that call to run each view.
import SwiftUI
struct TabBarView: View {
var body: some View {
TabView {
ContentView()
.tabItem {
Text("1")
//Image("TabIcon"+"Account")
Image(systemName: "list.bullet.rectangle.portrait")
}
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
ContentView2()
.tabItem {
Text("2")
//Image("TabIcon"+"Account")
Image(systemName: "list.bullet.rectangle.portrait")
}
.environment(\.managedObjectContext, PersistenceController2.shared.container.viewContext)
}
}
}
It seems that SwiftUI can not support multiple containers and error show like below.
2022-12-07 21:06:41.411772+0700 testCoreData[38345:1010632] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Item' so +entity is unable to disambiguate.
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Item' so +entity is unable to disambiguate.
2022-12-07 21:06:41.412037+0700 testCoreData[38345:1010632] [error] warning: 'Item' (0x60000083c630) from NSManagedObjectModel (0x600001c169e0) claims 'Item'.
CoreData: warning: 'Item' (0x60000083c630) from NSManagedObjectModel (0x600001c169e0) claims 'Item'.
2022-12-07 21:06:41.412175+0700 testCoreData[38345:1010632] [error] warning: 'Item' (0x6000008306e0) from NSManagedObjectModel (0x600001c105a0) claims 'Item'.
CoreData: warning: 'Item' (0x6000008306e0) from NSManagedObjectModel (0x600001c105a0) claims 'Item'.
2022-12-07 21:06:41.412302+0700 testCoreData[38345:1010632] [error] error: +[Item entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[Item entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2022-12-07 21:06:41.557326+0700 testCoreData[38345:1010632] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'
May you please give me suggestion, how to work with 2 CoreData containers ?
Note: In the real project, I may have many entities, such as 10 entities. So the suggestion from this below link is not practical for me. https://developer.apple.com/documentation/coredata/linking_data_between_two_core_data_stores