Hi CodeCrew,
The issue I have is when I created an instance of my CoreDataModel() in my UploadModel()… Reason I have to do this is because I have background task in my UploadModel() that uploads data to the database. This upload task runs in the background when a button is pressed… When each upload is done, it needs to add or delete the record in CoreData hence it needs to access the add/delete function in CoreDataModel…
I think solving this needs some advance Dev Skills hence asking for help…
Found the link below and I think it has the answers to my issue but I just don’t understand it.
/// ------------------------------------------------------------------------------------
/// Core Manager for Core Data..
/// ------------------------------------------------------------------------------------
final class CoreDataManager {
static let instance = CoreDataManager()
let container: NSPersistentContainer
let context: NSManagedObjectContext
let containerName: String = "UsersContainer"
init() {
container = NSPersistentContainer(name: containerName)
container.loadPersistentStores { (description, error) in
if let error = error {
print("❌ERROR|Error Loading Code Data|\(error)")
} else {
print("💡INFO|Success loading Core Data!")
}
}
context = container.viewContext
}
func save() -> Bool {
var retVal: Bool = true
do {
try context.save()
} catch let error {
print("ERROR|Core Data Save Failed!|\(error)")
retVal = false
}
return retVal
}
}
class CoreDataModel: ObservableObject {
@Published var forResubmission: [ForResubmissionEntity] = []
let coreDataManager = CoreDataManager()
private func saveForResubmission() {
// When save is successful, refresh data..
if coreDataManager.save() {
self.fetchResbumission()
}
}
func addResubmitToCoreData() {
let newSubmission = ResubmissionEntity(context: coreDataManager.context). **--> This is where I get all the warnings...**
.....
.....
self.saveForResubmission()
}
func deleteResubmit() {
.....
.....
coreDataManager.context.delete(ResubmitEntity)
self.saveForResubmission()
}
}
class UploadModel: ObservableObject {
private func postUploadFail() async throws {
let coreDataModel = CoreDataModel()
.....
.....
coreDataModel. addResubmitToCoreData()
}
private func postUploadSuccess() async throws {
let coreDataModel = CoreDataModel()
.....
.....
coreDataModel.deleteResubmit()
}
}
This are the warnings I get…
2023-09-26 14:32:13.777466+1000 MyApp[38658:811893] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'ForResubmissionEntity' so +entity is unable to disambiguate.
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'ResubmissionEntity' so +entity is unable to disambiguate.
2023-09-26 14:32:13.777717+1000 MyApp[38658:811893] [error] warning: 'ForResubmissionEntity' (0x6000034cc160) from NSManagedObjectModel (0x6000020e80a0) claims 'ForResubmissionEntity'.
CoreData: warning: 'ForResubmissionEntity' (0x6000034cc160) from NSManagedObjectModel (0x6000020e80a0) claims 'ForResubmissionEntity'.
2023-09-26 14:32:13.777887+1000 MyApp[38658:811893] [error] warning: 'ForResubmissionEntity' (0x6000034e2260) from NSManagedObjectModel (0x600002067020) claims 'ForResubmissionEntity'.
CoreData: warning: 'ForResubmissionEntity' (0x6000034e2260) from NSManagedObjectModel (0x600002067020) claims 'ForResubmissionEntity'.
2023-09-26 14:32:13.778054+1000 MyApp[38658:811893] [error] error: +[ForResubmissionEntity entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[ForResubmissionEntity entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
The flow is when a button is press: UploadFunc() ->postUploadFail()->postUploadFail()-> addResubmitToCoreData()… The line where I get the warnings is from below:
let newUserColl = ForResubmissionEntity(context: coreDataManager.context)
My guess is because I have created an instance of the CoreDataModel() in UploadModel() and the NSManagedObjectContext being created again somehow? Appreciate if you could help me on this…
Or if you could suggest how I can call a CoreDataModel func() without creating another instance of it…
Cheers,