0

我有 2 个实体:文章和类别。

//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"];

然后我想知道文章的类别,但它等于null:

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

article.categoryId 和 category.categoryId 相等。

为什么映射关系不起作用?

4

1 回答 1

0

用这种方式尝试关系映射

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

这对我来说很完美。我已经在我的两个项目中使用了它。

于 2013-04-11T06:30:36.783 回答