How to correctly remove the "temporary" NSManagedObject after the application exits

I create a temporary NSManagedObject and associate it with the main NSManagedObjectContext. I need to be able to consider it as a fully functioning object (execute queries for selection, etc.) inside the context and therefore cannot create it without an associated context. I include the logic for deleting a managed entity in ViewWillDisappear, provided that the new view controller was not just pushed onto the stack:

- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; NSArray *viewControllers = self.navigationController.viewControllers; if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { // View is disappearing because a new view controller was pushed onto the stack } else { // View is disappearing for some other reason [self.community.managedObjectContext deleteObject:self.community]; } } 

This is like properly deleting a managed entity in all cases, except for application failure. I tried deleting the object in viewDidUnload, but it seems that the method is not being called when the application exits. I considered creating a second context for managed objects, but I want, if possible, to avoid the main overhead.

Thanks Graham

+4
source share
4 answers

Store the managed entity in the shared instance as a class variable so that it is accessible from more places in your application and then from the class with which you are processing it.

There are two scenarios:

1) before iOS 4.0
When the WillTerminate application you can remove the object from the context.

2) Since iOS 4.0.
When your application enters the background and you kill this process (the hard way, using the kill bar panel), the WillTerminate application will never be called. You cannot recognize this event. So you rolled up the WillTerminate app.
You will have to solve it as follows:
- applicationWillEnterBackground: → save the managed entity identifier in userdefaults files or in a simple file.
- applicationDidFinishLaunching: → if the file exists, delete the managed entity to which it refers.
- applcationWillEnterForeground: → delete file.

Now, when your application goes into the background and returns, you will have the same state and the object will not be deleted. When you kill the application, the object will be deleted at startup.

+1
source

In your application, implement the -applicationWillTerminate: method. This method is called immediately before deleting your application from memory, and you can delete a temporarily managed object there.

0
source

applicationWillTerminate and its multi-tasking cousins ​​work, but since you delete only one object, the best way is to maintain your context after each removal.

Just call - (BOOL)save:(NSError **)error :

 [self.community.managedObjectContext deleteObject:self.community]; NSError *error = nil; [self.community.managedObjectContext save:&error]; 
0
source

I understand that you already have a working answer, but if your object is really temporary and will never be saved, why not just create it in a child context? It will still have all the appearance of the choice you desire, but it will never be redirected to other contexts and will not be stored in the physical database until you call save in context.

If you never call save, the temporary object will never be saved, which makes it temporary.

And the kicker ... you don’t need to write any additional code at all or handle all the “outgoing” conditions, since it never got into the actual database.

0
source

Source: https://habr.com/ru/post/1341465/


All Articles