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