Re: CoreData and mutableArrayValueForKey:
Re: CoreData and mutableArrayValueForKey:
- Subject: Re: CoreData and mutableArrayValueForKey:
- From: Alex Duzik <email@hidden>
- Date: Sat, 30 Aug 2008 21:37:42 -0500
On Aug 30, 2008, at 9:30 PM, Chris Hanson wrote:
On Aug 30, 2008, at 2:23 PM, Alex Duzik wrote:
I'm writing an app where one Core Data entity -- Folder -- has
(conceptually) a to-many relationship with another object -- Item.
But I want to keep those items in a particular order, so I have a
third entity, OrderedItem, which keeps a reference to the folder
along with a reference to the item and also adds an order attribute
to track the item's position in the folder.
This is rather cumbersome to maintain, so I've implemented the key-
value coding methods for a to-many relationship:
- (NSUInteger) countOfItems
- (Item *)objectInItemsAtIndex:(NSUInteger)index;
- (void)insertObject:(Item *)item inObjectsAtIndex:(NSUInteger)index;
- (void)removeObjectFromItemsAtIndex:(NSUInteger)index;
This works really well just about everywhere. However, in my window
controller's delete: method, calling
mutableArrayValueForKey:@"items" gives me this exception:
2008-08-30 16:11:27.718 MyApp[6436:813] *** -
[NSKeyValueFastMutableArray2 count]: value for key items of object
0x2a7dc0 is nil
Don't Do That. I suspect you have a relationship named "items" on
your Folder entity, but you're trying to implement a mutable array
property named "items" on the class used by your Folder entity. The
two will conflict with each other, leading to unpredictable results.
Keep the names distinct and you shouldn't have any such issue. For
example, I might name things like this:
Folder.items -- mutable-array property on Folder class
Folder.unorderedItems -- to-many relationship on Folder entity to
Item
Folder.orderedItems -- to-many relatiopnship on Folder entity to
OrderedItem
In my code, I'd probably use the Folder.items property most of the
time (for example, for bindings, or for otherwise manipulating the
set of items in a Folder) and just have its accessor methods do the
appropriate work to keep its Folder.unorderedItems and
Folder.orderedItems relationships in sync.
-- Chris
There is no "Items" relationship in the model. Only an "orderedItems"
relationship. "items" exists only as the KVC accessor methods, as you
recommended. In fact, I want 'items' to be the only public interface
(I'm basically trying to get around the fact that Core Data doesn't
provide a way of storing an ordered relationship) What's aggravating
is that mutableArrayValueForKey: works correctly in one method --
tableView:acceptDrop... but not on a different method of the very same
class -- deleteBackward: It gives the exception from my original post.
Same thread, same everything else, near as I can tell.
Alex
_______________________________________________
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