Reputation: 884
In view model I try to call two functions one of them create new list "addNewList" and in side this function I create a new sublist "addMainSublist" as shown in the code bellow, the "addNewList" function work perfectly. Also, when I try to call the "addMainSublist" inside "addNewList" the error in the photo shown inside "PersistenceController" file when I try to save the new sublists in CoreData, I call the "addNewList" inside SwiftUI view and take "moc" parameter from "@Environment(.managedObjectContext) private var managedObjectContext" property in the SwiftUI view.
Note: as you see in the code bellow I use the same "managedObjectContext" to save both new list and new sublist, when I erase all content in the simulator and try to create new list the error did not show but in the second try the error shown again.
func addNewList(moc: NSManagedObjectContext, mainLists: FetchedResults<ListOfTasks>, favoriteLists: FetchedResults<ListOfTasks>) {
let newList = ListOfTasks(context: moc)
newList.id = UUID()
newList.addedDate = addedDate
newList.title = title
newList.icon = icon
newList.color = Color.set(stringfor: color)
newList.origin_Group = origin_Group
if origin_Group == nil {
newList.state = ListState.list.rawValue
newList.index = Int16(mainLists.count)
newList.isFavorite = isFavorite
newList.favoriteIndex = Int16(isFavorite ? favoriteLists.count : 0)
}else{
if let wrappedGroup = origin_Group {
newList.state = ListState.sublist.rawValue
newList.index = Int16(wrappedGroup.listsArray.count-1)
newList.isFavorite = false
newList.favoriteIndex = 0
}
}
newList.isLocked = isLocked
newList.isArchived = isArchived
PersistenceController.shared.save()
addMainSublist(to: newList, moc: moc)
}
func addMainSublist(to newList: ListOfTasks, moc: NSManagedObjectContext) {
let mainSublist = Sublist(context: moc)
mainSublist.origin_List = newList
mainSublist.id = UUID()
mainSublist.addedDate = Date()
mainSublist.index = 0
mainSublist.title = "\(title)_MainSublist"
mainSublist.isExpanded = false
mainSublist.isArchived = false
PersistenceController.shared.save()
}
Error Photo
Upvotes: 0
Views: 964
Reputation: 21
Not the issue specifically here but in case on your search you come across this same error, check if you have your "one to many" or "one to one" relationship set correctly. That was my case where I forgot to change it to "one to many" when I set up the relationship between two entities. That solved my error.
Upvotes: 2
Reputation: 884
The answer by Joakim Danielson who gives me the note about "compare", the problem is using sort description "list.title" to fetch all sublists in the SwiftUI view for debugging, and when I change that the error is gone, thanks for "Joakim Danielson" for his valuable note.
Upvotes: 0