I have a variable that gets changed a lot that I have therefore pulled out of my core data database (it's just an int but it gets updated very regularly).
My app is NSPersistentDocument
based and when the user closes a document I would like to save this variable into the database so it persists.
I did the following:
- (void) windowWillClose:(NSNotification *)notification
{
[self.managedObjectContext performBlockAndWait:^(void) {
self.myDatabase.myNumber = [NSNumber numberWithInt:self.myTempInt];
[self.managedObjectContext save:nil];
}];
}
This works in terms of saving the data, and when a new document opens I can set myTempInt
from the database value.
However, it has introduced a problem in that the document no longer deallocates – it seems that referencing self.myDatabase
at the moment the document is closing creates a retain that means ARC never kicks in.
Is there a better place to do this kind of thing that won't be a problem for reference counting or am I approaching it all wrong?
Update:
I've been thinking this over and realised that it would be far better to do this operation whenever the document autosaves, rather than when it closes. Is there something like 'documentWillAutosave' I could use?