我有 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 相等。
为什么映射关系不起作用?