没有添加RestKit外键关系映射

时间:2014-12-02 12:17:24

标签: foreign-key-relationship restkit-0.20

关于RestKit和外键关系映射的问题很少。

在开始我的问题之前,这里有适当的工作和正常工作。

  1. 核心数据模型
  2. Data model

    收藏夹具有以下属性:

    placeIds:包含定义为用户最喜欢的地方的地点ID的可转换类型。

    1. RestKit
    2. 一个。用户数据映射

      RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
                                                           inManagedObjectStore:[RKManagedObjectStore defaultStore]];
      
      [entityMapping addAttributeMappingsFromDictionary:@{@"id":@"userId"}];
      entityMapping.identificationAttributes = @[ @"userId" ];
      [entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"favorites" withMapping:[AOFavorite mapping]]];
      
      [entityMapping setAssignsDefaultValueForMissingAttributes:YES];
      [entityMapping setAssignsNilForMissingRelationships:YES];
      

      湾最喜欢的数据映射

      RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
                                                           inManagedObjectStore:[RKManagedObjectStore defaultStore]];
      
      [entityMapping addAttributeMappingsFromDictionary:@{@"placeIds":@"placeIds"}];
      entityMapping.identificationAttributes = @[ @"userId" ];
      [entityMapping addConnection:[AOFavorite placesConnection]];
      
      [entityMapping setAssignsDefaultValueForMissingAttributes:YES];
      [entityMapping setAssignsNilForMissingRelationships:YES];
      
      + (RKConnectionDescription *)placesConnection
      {
          NSEntityDescription *entity = [NSEntityDescription entityForName:[self entityName]
                                                    inManagedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
          NSRelationshipDescription *places = [entity relationshipsByName][@"places"];
          return [[RKConnectionDescription alloc] initWithRelationship:places attributes:@{ @"placeIds": @"placeId" }];
      }
      

      ℃。放置数据映射

      RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
                                                           inManagedObjectStore:[RKManagedObjectStore defaultStore]];
      
      [entityMapping addAttributeMappingsFromDictionary:@{@"id":@"placeId"}];
      entityMapping.identificationAttributes = @[ @"placeId" ];
      
      [entityMapping setAssignsDefaultValueForMissingAttributes:YES];
      [entityMapping setAssignsNilForMissingRelationships:YES];
      

      用户实体已将登录的用户对象正确地放入持久存储中。

      由于API为用户最喜欢的地方提供了一系列ID,因此将它们存储到用户>收藏> placeIds

      -

      我使用以下方法请求API的对象获取收藏夹与Place定义的关系:

      [[[[RKObjectManager sharedManager] router] routeSet] addRoute:[RKRoute routeWithRelationshipName:@"favorite/places"
                                                                                           objectClass:[AOFavorite class]
                                                                                           pathPattern:@"user/favorite/places"
                                                                                                method:RKRequestMethodGET]];
      
      [[RKObjectManager sharedManager] getObjectsAtPathForRelationship:@"favorite/places"
                                                              ofObject:[[AOUser currentUser] favorites]
                                                            parameters:nil
                                                               success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
      
                                                               } failure:^(RKObjectRequestOperation *operation, NSError *error) {
      
                                                               }];
      
      1. 问题和疑问
      2. 一个。 getObjectsAtPathForRelationship具有正确创建到Place实体的位置,但收藏夹>地方关系没有联系。

        湾将placeIds放入Favorite实体的目的是什么?我已经看到关于外键映射的SO问题/响应,说明这是实现它的方法。有人可以解释为什么吗?

0 个答案:

没有答案