I want a "fake" preview NSManagedObject to display as an example. However, I don't want to have it cluttering up my regular context. Here's what I'm assuming could work:
Create a new
NSManagedObjectContextand store the previewNSManagedObjecttherelet moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) let person = Person(moc, "Person Name")What happens to the new context in this case? Is it slowly filling up storage or will it get removed if no
save()is called and the View removed?Remove the newly created
NSManagedObjectwhen leaving the viewSomeView() .onDisappear() { moc.delete(person) PersistenceController.shared.save() }This might not work if the
.onDisappear()function doesn't get called, e.g. when the user leaves the app while that particular view is still open.Add the fake preview
NSMangagedObjectto the regular context and filter it out everywhere else (probably the worst idea)Create a new global context only for fake
NSManagedObjects
Not sure which of these options is best or if there's a better way I haven't thought of. Thanks :)
I created a new PersistenceController in memory as Joakim Danielson suggested in the comments, but using it alongside my other context was causing crashes and error messages. Simply creating a new NSManagedObjectContext, however, worked fine.
It is not saved to my "real" context, either (Apple Documentation):
Usage