Re: Cleaning "garbage" in Core Data
Re: Cleaning "garbage" in Core Data
- Subject: Re: Cleaning "garbage" in Core Data
- From: BJ Homer <email@hidden>
- Date: Sun, 16 Aug 2009 21:22:42 -0600
On Sun, Aug 16, 2009 at 10:35 AM, Squ Aire <email@hidden> wrote:
>
> I am not *explicitly* marking anything as garbage. Whenever a user decides
> to remove a ValidKeys managed object, corresponding key-value pairs in all
> the userInfo dictionaries are *conceptually* marked as garbage. This is
> because I do not want to show the beach ball to the user while things are
> getting cleaned up. I want to postpone the cleaning up until later when it
> will not bother the user.
If the goal is to avoid beachballing the user, you may be able to simply put
the cleanup logic into a background thread and fire it off immediately.
Beachballs only happen when the main thread is blocked. Now, if this is
data that is displayed and manipulated in the UI, you may need to do some
synchronization to avoid race conditions. But in general, avoiding
beachballs is a matter of doing things in the background instead of delaying
it until later; if you wait to clean up garbage, you'll just postpone your
beachball until later. Now, there may still be merit to batching the
deletions; you could make a background thread that runs cleanup every 1.0
seconds on any object in a "deletion queue" or something.
I'm not a Core Data expert (yet), but you will need to give the background
thread a separate NSManagedObjectContext. As you mentioned, you'll need to
notify the main thread's MOC of the changes. Luckily, in 10.5 there's
-[NSManagedObjectContext
mergeChangesFromContextDidSaveNotification:]<http://developer.apple.com/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/doc/uid/TP30001182-SW9>,
which takes care of that. Register an observer on
the NSManagedObjectContextDidSaveNotification notification with the
background MOC as the object and the main MOC as the target, and whenever
the background thread performs a save, the main thread's MOC will be
updated.
That's how I'd do it, anyway.
-BJ
_______________________________________________
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