开发者

iOS / iPhone CoreData Relationships

I have been trying to find a good CoreData tutorial that focuses on more a开发者_JAVA百科dvanced databases that have one-to-many and many-to-many relationships. From what I've read, CoreData hides the middle table that would normally be in a many-to-many relationship. But my question is more about how to set the one-to-many relationships using CoreData.

A portion of my database consists of a Category table and an Item table. One category can have many items.

In my program I get to the point where the user Selects a category, then a list view pops up showing all the items that should be in that category.

I can add an item to the table perfectly fine, it's when I try to add an item to a table with a relationship to a category when problems/crashes arise.

Should the item be completely saved to the database first, then regrab it and set the relationship?

Some code:

[item.m]

@interface ITEM :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) CATEGORY * category;

@end

[/item.m]

[category.m]

@interface CATEGORY :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet* items;

@end


@interface CATEGORY (CoreDataGeneratedAccessors)
- (void)addItemObject:(item *)value;
- (void)removeItemObject:(item *)value;
- (void)addItems:(NSSet *)value;
- (void)removeItems:(NSSet *)value;

@end

[/category.m]

None of the CoreDataGeneratedAccessors are implemented by me, I assume they are already created and hidden.

I tried setting the relationship both ways, once from CATEGORY and once from ITEM i.e. [[self category] addItemObject:[self item]]; and also [[self item] setCategory:[self category]];

Neither worked... I got an error like this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'category' between objects in different contexts (source = <NSManagedObject: 0x614b660> (entity: item; id: 0x61558f0 <x-coredata:///item/tF691713B-18B1-4D28-93CC-40327353C08F3> ; data: {
    category = nil;
    name = TestName;

}) , destination = <CATEGORY: 0x4d55c20> (entity: category; id: 0x4d55050 <x-coredata://188D1CD9-6CC0-47EF-9C2A-A5DDDB1FAA24/category/p1> ; data: {
    items = "<relationship fault: 0x4da4800 'items'>";
    name = "Test Category";
}))'

So I guess the question would be: where am I going wrong, and is there a really good tutorial that walks through CoreData examples that have more than one table and actually have a one-to-many relationship (and what is the link)?


'Illegal attempt to establish a relationship 'category' between objects in different contexts

There is your problem. You can't have relationships between different contexts.

You can either refactor your code so that it uses only one NSManagedObjectContext or merge the item into the "main context" before you assign a category to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜