Post

Replies

Boosts

Views

Activity

I Keep getting this error in my swift project.
Since the iOS 18 and Xcode 16, I've been getting some really strange SwiftData errors when passing @Model classes around. SwiftData/BackingData.swift:409: Fatal error: This model instance was destroyed by calling ModelContext.reset and is no longer usable. PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://D0F0E233-8D1D-4020-924B-BA56959140FD/ListModel/p10), implementation: SwiftData.PersistentIdentifierImplementation) The same issue also happens when I try to retrieve a model from the ModelContext using its PersistentIdentifier and try to do anything with it. I have no idea what could be causing this. This is my actor @ModelActor actor ListCrudOperations:ObservableObject{ func add(list:ListModel){ modelContext.insert(list) try? modelContext.save() } func delete(identifier:PersistentIdentifier){ guard let list = self[identifier, as: ListModel.self] else { print("error") return } if list.listType == .task{ list.reminders!.forEach { reminder in TaskModel.delete(modelContext: modelContext, reminder: reminder) NotificationService.deleteReminders(name: reminder.title!, Id: reminder.id) } } else if list.listType == .subscription { list.subscription!.forEach({ sub in Subscription.delete(modelContext: modelContext, subscription: sub) NotificationService.deleteReminders(name: sub.ServiceName, Id: sub.id) }) } else if list.listType == .link { list.links!.forEach ({link in Links.delete(modelContext: modelContext, link: link) NotificationService.deleteNotificationForLink(title: link.name, linkID: link.id) } ) } modelContext.delete(list) try? modelContext.save() } func addReminder(reminder:TaskModel, identifier:PersistentIdentifier){ guard let list = self[identifier, as: ListModel.self] else { print("error") return } list.reminders!.append(reminder) reminder.list = list try? modelContext.save() } func addSubscription(subscription:Subscription, identifier:PersistentIdentifier){ guard let list = self[identifier, as: ListModel.self] else { print("error") return } list.subscription!.append(subscription) subscription.list = list try? modelContext.save() } func addLink(link:Links, identifier: PersistentIdentifier) { guard let list = self[identifier, as: ListModel.self] else { print("error") return } list.links?.append(link) link.list = list try? modelContext.save() } func fetchListByType(type:ListType) -> [ListModel] { let type = SwiftTaskSchemaV8.ListModel.ListType(rawValue: type.rawValue)! let fetchDescriptor = FetchDescriptor<ListModel>() do { let list = try modelContext.fetch(fetchDescriptor) let list2 = try list.filter(#Predicate { $0.listType == type }) return list2 }catch{ return [] } } func fetchListsForMultipleTypes(_ types: [ListType]) -> [ListModel] { return types.flatMap { type in fetchListByType(type: type) } } func fetchAllList() -> [ListModel] { let fetchDescriptor = FetchDescriptor<ListModel>(sortBy: [.init(\.createdDate)]) do { let list = try modelContext.fetch(fetchDescriptor) return list }catch{ return [] } } }``` and this is how i am calling it @Environment(.modelContext) private var context let listOperation = ListCrudOperations(modelContainer: context.container) let list = ListModel(name: name, color: self.color, icon: self.icon, listType: ListModel.ListType(rawValue: picked.rawValue)!) Task { await listOperation.add(list: list) await MainActor.run{ withAnimation(.bouncy){ self.list.append(list) } CrashServices.shared.addLogs(message: "folder added") } }
4
1
545
Sep ’24