【问题标题】:iOS Restkit how to relate entities in mappingiOS Restkit如何在映射中关联实体
【发布时间】:2013-04-03 03:57:12
【问题描述】:

我有 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 相等。

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

【问题讨论】:

  • [categoryMapping mapRelationship:@"articles" withMapping:articleMapping]; [articleMapping mapRelationship:@"category" withMapping:categoryMapping]; [articleMapping connectRelationship:@"category" withObjectForPrimaryKeyAttribute:@"categoryId"];这段代码对吗?

标签: ios objective-c mapping restkit relationship


【解决方案1】:

试试这种方式的关系映射

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

这对我来说是完美的。我在我的两个项目中都使用过它。

【讨论】:

  • 谢谢您的回答,但是您如何定义article.categoryId与categoryId相同?
猜你喜欢
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多