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
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I work with CoreData and need to backup/sync entire database to cloud server.
The main CoreData is work well to show data in each view.
But when I backup sqlite file to cloud server, then download sqlite file to another local location in iPhone, then I load NSPersistentContainer for this sqlite database file. Xcode shows "error: A fetch request must have an entity."
Note: I'm sure that one NSPersistentContainer is load one sqlite file, two NSPersistentContainer is not point to the same file.
Note2: This concept is work well in previous version of swift, but now I try to implement this concept with SwiftUI.
Please suggest me for some ideas and/or solutions.
I want to create multiple view with difference query data by "accountType".
By call: MyView(accountType: "Income") or MyView(accountType: "Expense")
This below is code of "MyView".
struct MyView: View {
@Environment(\.managedObjectContext) private var viewContext
var accountType = "Income"
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \JMAccount.displayOrder, ascending: true)]
, predicate: NSPredicate(format: "(type == %@)"
, argumentArray: [accountType] // With error
//, argumentArray: [Int(2), "Expense"] // No error
)
, animation: .default
)
private var items: FetchedResults<JMAccount>
var body: some View {
NavigationView {
List {
ForEach(items) { item in
Text(item.name!)
}
}
}
}
}
Xcode shows error message at "argumentArray: [accountType]"
Anyone has a solution for this issue? Please give me an advise.
Here is my code ...
var accountType = "Account"
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \JMAccount.displayOrder, ascending: true)]
, predicate: NSPredicate(format: "(hidden < %@) AND (type == %@)", argumentArray: [Int(1), accountType])
, animation: .default
)
private var items: FetchedResults<JMAccount>
Then Xcode report error below ...
"Cannot use instance member 'accountType' within property initializer; property initializers run before 'self' is available"
How can I solve this error ?
More info, I want to make multiple view with parameter to @FetchRequest. So I try to make variable for this parameter.
If you have better idea, please suggest me.