Re: Unique items in Core Data models
Re: Unique items in Core Data models
- Subject: Re: Unique items in Core Data models
- From: "John R. Timmer" <email@hidden>
- Date: Fri, 18 May 2007 08:58:54 -0400
Managed objects already have a unique ID -- the objectID.
The issue is, how do you subsequently ensure uniqueness of a
particular attribute...
Came in late to the discussion, and don't know whether this has been
suggested...
I solved this by creating a singleton, non-managed object. When i
hand it a ManagedObjectContext, it fetches all the appropriate
objects into an array, traverses the key path to create an array of
the appropriate attribute, and then makes that array into a set
(since that's faster for object presence tests). It registers with
the ManagedObjectContext for new object creations and attribute
changes, and updates the set accordingly.
It has a method to check the set for any key you want to add, which i
use during validations of keys. I thought it was a nice way to clump
all the relevant code in a single class, and since entity names and
keys are strings, it's easy to generalize the code so you can
configure the singleton differently in different projects. I don't
think there's any way to speed up updating the set, but if you find
the presence test slow, you can change it to an all-C function,
treating the set as a CFSet.
JT
____________________________________
Science Writer
Ars Technica - http://arstechnica.com/journals/science.ars
Wellcome Science - http://www.wellcome.ac.uk/wellcomescience
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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