> When I create a document, save it, then save as, then save as again, it 
> duplicates the persistent store, so the managed objects I have been using in 
> my application are all invalidated. 
> 
> Now there are a whole lot of places in my application where I have KVO set up 
> on properties of the managed objects, and some places where I have the 
> managed objects set as instance variables of my objects. I was not expecting 
> them to be invalidated by the save as operation. 

They are not invalidated as part of Save.  They are as part of Save As since 
that creates a new document, with a new persistent store, a new coordinator, 
new pretty much everything.  Save As is creating a duplicate, so the original 
content still exists.  However, the UI needs to be bound to the new content.  
Think of the behavior of TextEdit.  Save As is like "close old, duplicate, open 
new"

> And created a method to refresh my data:
> 
> 
> - (void)managedObjectsChanged:(NSNotification *)notification {
>       if ([[notification userInfo] objectForKey:NSInvalidatedObjectsKey]) {
>               NSSet *objects = [[notification userInfo] 
> objectForKey:NSInvalidatedObjectsKey];
>               for (NSManagedObject *obj in objects) {
>                       [[self managedObjectContext] refreshObject:obj 
> mergeChanges:YES];
>               }
>       } else if ([[notification userInfo] 
> objectForKey:NSInvalidatedAllObjectsKey]) {
>               for (NSManagedObject *obj in [[self managedObjectContext] 
> registeredObjects]) {
>                       [[self managedObjectContext] refreshObject:obj 
> mergeChanges:YES];
>               }
>       }
> }
> 
> I have confirmed that this is called in the save/save as/save as scenario, 
> with the NSInvalidatedAllObjectsKey, and my object is getting sent the 
> refreshObject message...But when I go to access my data, it still tells me 
> that it has been invalidated. Am I doing something wrong here?

Yes.  You cannot resurrect invalidated objects.  They are toast.  That's why 
you're getting an exception.  Nor would you want to in this case as they all 
represent content from the old (closed) document not the new saved as document. 
 You need to toss all these references and then go through the regular "I just 
opened a new document" code path for your newly saved document to reconstruct 
the KVO observations.

The canonical solution is to tell your controller objects like 
NSArrayController to refetch from the new document.   It sounds like you're 
fighting the document based Save As behavior.  It's "close the existing 
document, save the current state to a new document, open new document"

- Ben

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to