Re: Core data autosave
Re: Core data autosave
- Subject: Re: Core data autosave
- From: Gideon King <email@hidden>
- Date: Sat, 22 Jan 2011 11:20:57 +1000
Hi Ken, I have also been wrestling with this one, and last year had the following suggestion from Ben:
--------
"The problem with autosave is that NSDocument wants the current document to stay dirty. Save As pushes all the current edits to disk in the new document. Autosave is supposed to leave all the current edits as unsaved pending changes, even as it also saves those pending changes to a separate file.
There's no way to make an NSManagedObjectContext save, but leave everything dirty in memory.
With some effort, you could replicate the current pending changes into a new stack and save them aside. You could copy your existing document to a temp location, and then create a new PSC and new MOC and load the copied persistent store. Then you could walk through all the inserted, deleted, and updates objects in the user's main NSManagedObjectContext and duplicate the changes in your new MOC. You can clone over attributes directly with -dictionaryWithValuesForKeys and -setValuesForKeysWithDictionary. You'll need to handle relationships by hand by asking for each related objects NSManagedObjectID and using "objectWithID" to rematerialize it in your new MOC. Very tedious, although it should be straight forward if the autosave functionality is important."
--------
So, a slightly different approach and a little more detail there, but I kind of put it in the "too hard basket" for the moment because of all the stuff I would have to learn and implement, and was also concerned by the overhead of all that work on the performance of my application.
I will need to implement it shortly, so am very keen to hear of a solution - especially if that comes with some code that does the job ;-).
BTW, I filed a bug on this because I think that autosave should be basic functionality supported by core data.
Regards
Gideon
On 22/01/2011, at 11:01 AM, Kenneth Baxter wrote:
> I need to autosave my documents in my core data document based application
>
> I must say I was surprised and disappointed when I found out that Core Data doesn't appear to have any autosave support built in and NSPersistentDocument doesn't support the superclass NSDocument autosave architecture.
>
> I see there has been some discussion previously about possible approaches to solving this issue. I need something that works on 10.5 and 106.
>
> In 2008, Ben Trumbull said "You could fake it by migrating the store to the backup location, creating a second context, pulling all the deltas from the original, save, and then tossing the second context."
>
> Is this the current opinion on the best way to do autosave? It sounds like a very heavy approach, and I'm not sure on the actual implementation details of how to do what Ben says. If there is some common way of doing this, is there some sample code out there from Apple or someone else?
>
> Any feedback/suggestions would be very much appreciated.
>
> Thanks
>
> Ken_______________________________________________
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden