Learn Courses My Dashboard

Core Data missing on app restart

I’m using a near identical Core Data structure to that taught in the Recipe App to store some text data in a new app. This worked for a long time but at some point in my recent development of the app, I have found the FetchRequest is returning nil results when the app is rebuild and re-run after a modification. I can continue development only by deleting the app (and it’s data) and re-building from scratch.

I think (but am not 100% sure) that the problem began when I implemented a delete function for an element of CoreData but it now happens whether I actually delete data or not. I can add and delete data while running one build of the app, and come in and out of views that generate new FetchRequests and see the updated data so it looks like the save() operation is working. The problem only appears when I re-build and override the app - it returns nil results for the FetchRequest.

Any advice on how to start tracking down where the problem might be? I haven’t been able to find much advice on how to debug Core Data problems…

Hi,

I would start by setting a breakpoint and stepping through each line of code. Does each line do what you want it to do?

You can also pop your code into the discussion here and we can take a look at your delete and fetch method.
Blessings,
–Mark

Thanks - I’ve been stepping through and the only anomaly I can find is that if I try and check the value of moc from this statement in the first view called in the app it crashes when being run after the first time with an unhandled exception (when it’s ok on first run):

@Environment(.managedObjectContext) var moc

One oddity I found today - if I delete the app, close the simulator and Xcode and restart the iMac, everything works fine. The problems seems to occur when Xcode the simulator have been open for some time…

That seems like a bug in Xcode and Swift. OR a memory leak