SwiftData - Context missing for optional

I have encountered an issue that when using a ModelActor to sync data in the background, the app will crash if one of the operations is to remove a PersistentModel from the context.

This is running on the latest beta of Xcode 16 with visionOS 1.2 as target and in Swift 6 language mode.

The code is being executed in a ModelActor.

The error is first thrown by: #5 0x00000001c3223280 in PersistentModel.getValue<τ_0_0>(forKey:) ()

Thread 1: Fatal error: Context is missing for Optional(SwiftData.PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://97AA86BC-475D-4509-9004-D1182ABA1922/Reminder/p303), implementation: SwiftData.PersistentIdentifierImplementation))

func globalSync() async {
        
        await fetchAndSyncFolders()
        
        let result = await fetchReminders()
        switch result {
        case .success(let ekReminders):
            var localReminders = (try? await fetch(FetchDescriptor<Reminder>())) ?? []
            
            // Handle local reminders with nil ekReminderID by creating new EKReminders for them
            for reminder in localReminders {
                if reminder.ekReminderID == nil {
                    await self.createEkReminder(reminder: reminder)
                }
            }
            
            // Re-fetch local reminders to include newly created EKReminderIDs
            localReminders = (try? await fetch(FetchDescriptor<Reminder>())) ?? []
            
            var localReminderDict = [String: Reminder]()
            for reminder in localReminders {
                if let ekReminderID = reminder.ekReminderID {
                    if let existingReminder = localReminderDict[ekReminderID] {
                        self.delete(model: existingReminder)
                    } else {
                        localReminderDict[ekReminderID] = reminder
                    }
                }
            }
            
            let ekReminderDict = createReminderLookup(byID: ekReminders)
            
            await self.syncReminders(localReminders: Array(localReminderDict.values), localReminderDict: localReminderDict, ekReminderDict: ekReminderDict)
            
            // Merge duplicates
            await self.mergeDuplicates(localReminders: localReminders)
            
            save()
            
        case .failure(let error):
            print("Failed to fetch reminders: \(error.localizedDescription)")
        }
    }
Answered by DTS Engineer in 797958022

Thread 1: Fatal error: Context is missing for Optional(SwiftData.PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://97AA86BC-475D-4509-9004-D1182ABA1922/Reminder/p303), implementation: SwiftData.PersistentIdentifierImplementation))

I am pretty sure that simply deleting a model object using (or not) a model actor (ModelActor) doesn't trigger the mentioned crash.

This error indicates that your model object doesn't have a valid model context, which can happen when you hold a SwiftData model object in a SwiftUI view and continue to access the object (as the view is updated) after it was deleted.

If that is not your case, please share a minimal project, with detailed steps, to reproduce the issue. I'll be interested in taking a look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

same issue here.

I am getting the same problem. Although I am not using a ModelActor to remove my objects, I get the same error when removing an object via a context menu in my SwiftUI view. In theory, calling modelContext.delete should run without any issues; I am inclined to believe this is a bug.

Same issue here. The same code works in iOS 17 but is broken in iOS 18.

Thread 1: Fatal error: Context is missing for Optional(SwiftData.PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://97AA86BC-475D-4509-9004-D1182ABA1922/Reminder/p303), implementation: SwiftData.PersistentIdentifierImplementation))

I am pretty sure that simply deleting a model object using (or not) a model actor (ModelActor) doesn't trigger the mentioned crash.

This error indicates that your model object doesn't have a valid model context, which can happen when you hold a SwiftData model object in a SwiftUI view and continue to access the object (as the view is updated) after it was deleted.

If that is not your case, please share a minimal project, with detailed steps, to reproduce the issue. I'll be interested in taking a look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I was running into this error when trying to delete an object from SwiftData when using an actual device on iOS 18 beta but not on a simulator running iOS 17.

My model did have a @Relationship property attached to it as well.

I added the following code when inserting an object and haven't gotten this crash the 10+ times I tried. Not idea if it's just a random coincidence or a work around for some bug.

 do {
    try modelContext.save()
 } catch {
    print(error)
 }

I'm also getting this crash as well on iOS18 simulators in Xcode 16 b4 but not on iOS17 simulators, it looks related to deleting a object from within a context menu I've opened up a feedback with the attached project FB14672741

I'm having the same issue. Things I tried (none of them worked):

  • Remove .cascade delete rule
  • Remove all children-type entities before removing the parent-type entity
  • .swipeActions as well as .onDelete

I also tried changing the method of deletion to the following. This tries to delete entity Object that has a to-many relationship children: [Child].

    guard let id = object.id else {
      return
    }
    do {
      try container.mainContext.delete(model: Object.self, where: #Predicate { object in
        object.id == id
      })
    } catch {
        print(error)
    }

This does not crash the app, but fails to delete the entity with the following console output:

Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={Reason=Entity named:Object not found for relationship named:children, MissingEntity=Object. (<NSEntityDescription: 0x6000035100b0>) ...

I'm not sure what this means, perhaps someone more clever will find it useful. @DTS Engineer

Im having the same issue..I delete an object in ios 18 on my physical device it crashes with :

SwiftData/BackingData.swift:251: Fatal error: Context is missing for Optional(SwiftData.PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://92F8A7FF-1C0A-4099-8654-BCE97E36325D/ExerciseSession/p19), implementation: SwiftData.PersistentIdentifierImplementation))

when I test the same thing on ios17 in a simulator and physical divice it works perfectly fine. Im inclined to agree that this is a swiftData bug in ios18

I was stuck on this too. Turns out I needed to update my @Relationship to accurately describe what was happening. For example, when I deleted an item I would get this crash until I added a deleteRule in my relationship.

Anytime ive gotten SwiftData crashes that were hard to debug, it's always been the @Relationship so far

Hello all, I'm facing the same issue - works fine on iOS 17. Crash on iOS 18 beta 5 (iOS and Xcode). Happens on device and simulator (tested on these platforms - iPhone, iPad, watchOS)

I have the following SwiftData structure. CloudKit configuration - Room Session

ModelContext activity is handled on SwiftUI view (create model, insert, save)

Room has cascade array of Session.

Initial state: No sessions. Create new session and append it to room's sessions. Delete the session. I check the state now - room is exist with empty sessions, as expected Create a new one. Crash on -

        _$observationRegistrar.access(self, keyPath: \.room)
        return self.getValue(forKey: \.room)
    }

I print the PersistentIdentifier before the crash and everything seems to be ok -


PersistentIdentifier(id: CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate managedObjectContextSaved:]: <NSCloudKitMirroringDelegate: 0x303ab00f0>: Observed context save: <NSPersistentStoreCoordinator: 0x302ea1ce0> - <NSManagedObjectContext: 0x303ea5110>

SwiftData.PersistentIdentifier.ID(url: x-coredata://ED065B2F-2704-46AD-88E1-4F1BE2BA7098/Session/p44), implementation: CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExporter analyzeHistoryInStore:withManagedObjectContext:error:]: <PFCloudKitExporter: 0x302598370>: Exporting changes since (0): <NSPersistentHistoryToken - {

SwiftData.PersistentIdentifierImplementation) SwiftData.PersistentModel.persistentModelID.getter : SwiftData.PersistentIdentifier

PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://ED065B2F-2704-46AD-88E1-4F1BE2BA7098/Room/p8), implementation: SwiftData.PersistentIdentifierImplementation)

PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://ED065B2F-2704-46AD-88E1-4F1BE2BA7098/Room/p8), implementation: SwiftData.PersistentIdentifierImplementation)


After the crash I open my app again, the session appears and everything works as expected.

Hope this helps.

Thanks a lot, Dudi

Is there an update on this? I still haven't found a fix.

This is still an issue for me on the XCode RC. Unsure of what to even look for to debug. Anybody else find a fix or any temporary workarounds? 

I kind of missed the replies and comments here because the Safari notifications didn't quite work for me :-(. Now I am picking up thanks to folks who reached out me directly.

What is the recommended way of handling the situation if a SwiftUI View is holding a reference to the Model but the model gets deleted?

I typically consider the following patterns:

  1. Avoid deleting an object in the view that accesses the information of the object. If I have a list view that lists some objects and a detail view to show the details of the selected object, I'd put the delete feature in the list view (so users can swipe to delete), and not in the detail view. That way, when an object is deleted, there is no way to go into the detail view that uses the object.

  2. For views that need to hold an object that can potentially be deleted externally, make the object an optional, and nullify it when I do the deletion.

If you delete an object and save the deletion, but see that the object still exists in a relationship of the other object, meaning that the delete rule doesn't work, I'd suggest that you file a feedback report with a reproducible case, and share the report ID here.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Also getting this error in Xcode RC 16. It seems happen whenever I call save after a delete.

modelContext.delete(transaction)
try? modelContext.save()

It does not seem to matter if the delete comes from within a component or from a ForEach.onDelete it yields the same error.

SwiftData/BackingData.swift:251: Fatal error: Context is missing for Optional(SwiftData.PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://13C3453D-78A0-423B-841D-FF28F6EB2B24/Transaction/p998), implementation: SwiftData.PersistentIdentifierImplementation))
SwiftData - Context missing for optional
 
 
Q