Re: How do you store an NSArray as an attribute in a Core Data entity
Re: How do you store an NSArray as an attribute in a Core Data entity
- Subject: Re: How do you store an NSArray as an attribute in a Core Data entity
- From: Mic Pringle <email@hidden>
- Date: Thu, 19 Mar 2009 12:09:11 +0000
You can do this quite easily ... just set the entity type as data, and
then leave the archiver as default. Then when you want to use the
array you just have to cast it into the correct type, and CoreData
will handle the unarchiving for you i.e
NSArray *myArray = [myManagedObject valueForKey:arrayKey];
Hope this helps.
-Mic
2009/3/19 Chris Hanson <email@hidden>:
> On Mar 19, 2009, at 1:26 AM, email@hidden wrote:
>
>> I want to store a list of Strings as an attribute of an entity in Core
>> Data but there doesn't appear to be a way to use NSArray (or NSSet or
>> NSDictionary either) as an attribute.?Please can someone explain how you use
>> an NSArray or NSDictionary as an attribute of a Core Data entity?
>
> In general, if you want to have multiple things as an attribute of a Core
> Data entity, what you really want is a to-many relationship with another
> entity.
>
> Let's take tags as an example. Say I'm creating a weblog editor and I want
> the ability to tag my posts. I could model this as a string attribute with
> some specific format (space- or comma-separated), or as a transformed
> attribute. However, that doesn't get me the ability to do things like say
> "show all the posts with this tag" or even "auto-complete known tags." It
> also leads to persistent store bloat because I'm breaking a cardinal rule of
> relational data, "store each piece of data once," because each tagged post
> winds up with a copy of the tag's text.
>
> Instead of having an attribute, I would model a to-many relationship from my
> Post entity to my new Tag entity, with a to-many inverse relationship from
> my Tag entity to my BlogPost entity. This way:
>
> Each Tag instance only exists once in the persistent store.
> I can traverse the object graph between Posts and Tags, and between Tags and
> Posts, with ease.
> I can perform interesting queries across both Posts and Tags to improve my
> user experience (e.g. support auto-completion or generate tag clouds).
> Not having Tags stored directly on Posts means if I'm just dealing with a
> Post, Core Data won't bother fetching the Tags too.
>
> Entities and relationships are good, hope this helps you leverage them!
>
> -- Chris
>
> _______________________________________________
>
> 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
>
_______________________________________________
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