2013-04-11 93 views
0

我有2個實體:Article和Category。iOS Restkit如何關聯映射實體

//Article mapping 
RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore: objectManager.objectStore]; 
categoryMapping.primaryKeyAttribute = @"categoryId"; 
[categoryMapping mapKeyPathsToAttributes: 
@"CategoryId",@"categoryId", 
@"CategoryName",@"categoryName", 
nil]; 

//Category mapping 
RKManagedObjectMapping *articleMapping = [RKManagedObjectMapping mappingForClass:[Article class] inManagedObjectStore: objectManager.objectStore]; 
articleMapping.primaryKeyAttribute = @"articleId"; 
[articleMapping mapKeyPathsToAttributes: 
@"ArticleId",@"articleId", 
@"ArticleName",@"articleName", 
@"ArticleText",@"articleText", 
@"CategoryId",@"categoryId", 
nil]; 

//relationship mapping 
[categoryMapping mapRelationship:@"articles" withMapping:articleMapping]; 
[articleMapping mapRelationship:@"category" withMapping:categoryMapping]; 
[articleMapping connectRelationship:@"category" withObjectForPrimaryKeyAttribute:@"categoryId"]; 

//set mapping 
[objectManager.mappingProvider setMapping:articleMapping forKeyPath:@"article"]; 
[objectManager.mappingProvider setMapping:categoryMapping forKeyPath:@"category"]; 

那麼我想知道文章的範疇,但它等於空:

Article* article = [[Article allObjects] objectAtIndex:0]; 
    //article.category == nil 

article.categoryId和category.categoryId是相等的。

爲什麼映射關係不起作用?以這種方式

[categoryMapping mapKeyPath:@"articles" toRelationship:@"articles" withMapping:articleMapping]; 
[articleMapping mapKeyPath:@"category" toRelationship:@"category" withMapping:categoryMapping]; 

+0

[categoryMapping mapRelationship:@「articles」withMapping:articleMapping]; [articleMapping mapRelationship:@「category」withMapping:categoryMapping]; [articleMapping connectRelationship:@「category」withObjectForPrimaryKeyAttribute:@「categoryId」]; 此代碼是正確的? – Maxim 2013-04-11 05:32:12

回答

0

嘗試關係映射這是工作非常適合我。我在兩個項目中使用了它。

+0

謝謝你的回答,但你如何定義article.categoryId與category.categoryId相同? – Maxim 2013-04-11 08:45:59