CoreData delete clean up
CoreData delete clean up
- Subject: CoreData delete clean up
- From: Jim Thomason <email@hidden>
- Date: Fri, 24 Apr 2009 13:50:25 -0500
I just can't find a solution for this. Basically, I need a pre-delete
hook in CoreData but can't seem to find one.
Here's the deal - I have a Foo object which has many Bar objects. Foo
also has a property that's calculated off of the various values in
Bar.
So Bar has many custom set<Key> methods that twiddle the value up in
the Foo object to keep it up to date. All works well and good.
The problem is if I delete a Bar object. I need a way to have the Bar
object revert the cached Foo property to what it would've been had it
never existed. Basically, a pre-delete cleanup hook.
So what happens is, upon delete it marches through all of my set<Key>
methods in some order to wipe them out and screws up my cached
property. Basically, it ends up backing out the Bar object multiple
times, since while it's knocking out all of the properties, it doesn't
know that the object is actually deleted. Boom. Big mess.
Problems and caveats:
* I can't just have Foo monitor removeBarObject because it's not
guaranteed to wipe out the relationship first. It may have called a
half a dozen custom set<Key>s in Bar first so I'm still in some
inconsistent state.
* I have to have Bar notify Foo of changes (as opposed to having Foo
observe all of Bar's related properties) since there are relatively
few Foo objects and a -lot- of Bars. Further, the Bars only rarely
change their value. So to have a few Foos monitoring tons of Bars all
the time is just going to create a whole bunch of additional overhead
that I don't want.
* I have to store the property up in Foo, since it can also be
manipulated via other means. That is, I can't just replace the
calculated property with keyPath monitoring of the Bars.
* validateForDelete is called way too late in the process. I watched
it. It's very helpfully called after my object data is wiped out, all
of my set<Key>s are called, and my object's a mess.
* likewise for isDeleted. Doesn't work as a flag.
Possibilities:
* I can add an explicit "flagForDeletion" method to my Bar object and
explicitly call this every time I'm going to delete one. This seems
error-prone to me, since I may forget to put it somewhere. Further, it
makes it difficult to use in abstract situations unless I add dummy
flagForDeletion flags to all of my objects just to ensure that I can
always call the method. I know I can do it with a category, but still.
Likewise, I can add that into my ArrayController to handle the
majority of the cases.
For now, I've gone with that approach. the ArrayController's
removeObjectAtArrangedIndex: method calls flagForDeletion, which for
most objects does nothing, but for the Bars properly updates Foo's
property and then flags them as trash so the set<Key> values won't
update.
I hate having the extra method, though. Is there any better approach I
can use? With in the constraints of the above caveats, of course.
-Jim...
_______________________________________________
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