Core data RestKit addConnectionForRelationship无法识别连接

Core data RestKit addConnectionForRelationship无法识别连接,core-data,restkit,restkit-0.20,Core Data,Restkit,Restkit 0.20,我有一个RKEntityMapping对象,它将具有一对一关系的对象映射到子对象 调用以下代码时: [sessionMapping addConnectionForRelationship:@“dailyGoalDetail”连接者:@{@“dailyGoalDetail”:@“dailyGoalDetail”}]; 通过addConnectionForRelationship方法进行调试,我看到添加到连接集合中的连接是nil。 我通过以下方式找到与核心数据的连接进行了双重检查: NSEn

我有一个RKEntityMapping对象,它将具有一对一关系的对象映射到子对象

调用以下代码时:


[sessionMapping addConnectionForRelationship:@“dailyGoalDetail”连接者:@{@“dailyGoalDetail”:@“dailyGoalDetail”}];

通过addConnectionForRelationship方法进行调试,我看到添加到连接集合中的连接是nil。 我通过以下方式找到与核心数据的连接进行了双重检查:


NSEntityDescription*parentEntity=[NSEntityDescription entityForName:@“Session”在ManagedObjectContext:[MGRModel sharedModel].managedObjectStore.persistentStoreManagedObjectContext]中;
nsrrelationshipsdescription*childRelationship=[parentEntity relationshipsByName][@“dailygoaldeail”];
[sessionMapping addConnectionForRelationship:childRelationship被连接者:@{“DailyGoaldtaild”:@“DailyGoaldtaild”}];

登录到控制台我的关系显示了这一点。但是addConnectionForRelationship方法仍然将nil连接添加到其集合中

这个怎么了

更新

以下是我的会话对象的相关代码:

+ (RKEntityMapping *)responseMapping
{
    RKEntityMapping* sessionMapping = [RKEntityMapping mappingForEntityForName:@"Session" inManagedObjectStore:[[MGRModel sharedModel] managedObjectStore]];
    [sessionMapping addAttributeMappingsFromDictionary:@{
                                                         @"ActivityId": @"activityType",
                                                         @"DailyGoalDetailId": @"dailyGoalDetailId",
                                                         @"DeviceGuid": @"deviceId",
                                                         @"DeviceSessionId": @"entityId",
                                                         @"Frequency": @"frequency",
                                                         @"Pace": @"pace",
                                                         @"Start": @"start",
                                                         @"Stop": @"stop",
                                                         @"TotalBeats": @"totalBeats",
                                                         @"TotalKcal": @"totalCalories",
                                                         }];
    sessionMapping.identificationAttributes = @[ @"entityId" ];

    // Add relationship mapping with Measurement as "sessionDetails"
    [sessionMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"SessionDetails"
                                                                                   toKeyPath:@"sessionDetails"
                                                                                 withMapping:[MGRMeasurement responseMapping]]];

    NSEntityDescription *parentEntity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:[MGRModel sharedModel].managedObjectStore.persistentStoreManagedObjectContext];
    NSRelationshipDescription *childRelationship = [parentEntity relationshipsByName][@"dailyGoalDetail"];
    [sessionMapping addConnectionForRelationship:childRelationship connectedBy:@{@"dailyGoalDetailId":@"dailyGoalDetailId"}];

//    [sessionMapping addConnectionForRelationship:@"dailyGoalDetail" connectedBy:@{ @"dailyGoalDetailId": @"dailyGoalDetailId" }];

    return sessionMapping;
}
下面是这段关系的po:

(lldb) po childRelationship
(<NSRelationshipDescription: 0xc266570>), name dailyGoalDetail, isOptional 1, isTransient 0, entity Session, renamingIdentifier dailyGoalDetail, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, destination entity DailyGoalDetail, inverseRelationship sessions, minCount 0, maxCount 1, isOrdered 0, deleteRule 1
以及初始化和调用映射器的相关代码(这是通过NSString完成的):


RKMapperOperation
实际上做不了多少工作。在内部,它使用
RKMappingOperation
,这就是连接的原因。要使其正常工作,您有两个选项:

  • 使用2个相互依赖的实例,会话映射器以秒为单位运行
  • 使用
    RKObjectManager
    监督整个过程

  • 如果直接使用映射器操作,则需要其中的两个,以便可以显式控制顺序。理论上,您也可以将连接添加到两个映射上,因此,先运行哪一个并不重要(只要它们都由同一个MOC支持)。

    显示如何登录的代码test@Wain我已经添加了您要求的信息。如果您需要更多信息,请告诉我。这样代码看起来就可以了。目前还不清楚哪些不起作用(创建映射或映射结果)。不清楚为什么要直接使用
    rkmapperooperation
    (而不是
    RKObjectManager
    )。您是否验证了
    parsedData
    是否确实包含您期望的内容?然后打开跟踪日志记录,看看它对关系的描述。@Wain我一直在使用
    RKMapperOperation
    ,因为我一直在遵循那里的示例代码:-我无法粘贴日志,因为它是一个巨大的JSON。如果你想看看里面发生了什么,我可以把它放在dropbox上。据我所知,这种联系完全被忽视了。我尝试在子对象上添加一个连接,尽管连接尚未建立,但至少映射器说明了如何以这种方式评估连接。是否有可能我应该强制映射器先使用DailyGoalDetails数组,然后仅在映射该数组之后才使用该数组,处理UserSessions数组以便它可以找到相关数据?
    {
      "DailyGoalDetails": [
        {
          "DailyGoal": {
            "ActivityUnit": {
              "ActivityUnitId": 1,
              "Name": "BEATS"
            },
            "DailyGoalId": 1,
            "ActivityUnitId": 1,
            "Name": "Un inizio facile",
            "Beats": 25,
            "Level": 1
          },
          "DailyGoalDetailId": 6,
          "DailyGoalId": 1,
          "UserId": "7a2e5b7c-9a3a-4ad1-ad79-946bcf2bf2f3",
          "Hit": true,
          "Value": 9,
          "GoalDate": "2014-04-16T00:00:00",
          "AvgActivityTime": 12
        }
      ],
      "UserSessions": [
        {
          "UserSessionId": 0,
          "DeviceGuid": "962d26a01cdef0c8",
          "DeviceSessionId": "2a0bb7a0-041d-4df6-abcb-e50483858cba",
          "DailyGoalId": 1,
          "DailyGoalDetailId": 6,
          "ActivityId": 1,
          "Start": "2014-04-16T16:10:11",
          "Stop": "2014-04-16T16:15:53",
          "TotalBeats": 0,
          "TotalKcal": 1,
          "Pace": 0.00,
          "Frequency": 1,
          "SessionDetails": [
            {
              "Speed": 0.0,
              "Step": 4,
              "Distance": 0.0,
              "Beats": 0.0033395835198462009,
              "Lat": 45.70608193,
              "Lng": 9.70917967,
              "Altitude": 298.0,
              "Orientation": 0.0,
              "Start": "2014-04-16T16:10:10",
              "Stop": "2014-04-16T16:10:11"
            },
            {
              "Speed": 0.0,
              "Step": 2,
              "Distance": 0.0,
              "Beats": 0.0033395835198462009,
              "Lat": 45.70608193,
              "Lng": 9.70917967,
              "Altitude": 298.0,
              "Orientation": 0.0,
              "Start": "2014-04-16T16:10:11",
              "Stop": "2014-04-16T16:10:12"
            },
          ]
        }
      ]
    }
    
            // Json decoding
            NSError *decodingError = nil;
            id parsedData = [RKMIMETypeSerialization objectFromData:decodedUncompressedData MIMEType:RKMIMETypeJSON error:&decodingError];
            if (parsedData == nil && error) {
                NSLog(@"Error decoding json received from synchronization");
            }
            NSMutableDictionary *mappingsDictionary = [[NSMutableDictionary alloc] init];
            // Add DailyGoalDetails->MGRDailyGoalDetail mapping
            [mappingsDictionary setObject:[MGRDailyGoalDetail responseMapping] forKey:@"DailyGoalDetails"];
            // Add UserSessions->MGRSession mapping
            [mappingsDictionary setObject:[MGRSession responseMapping] forKey:@"UserSessions"];
            RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
            RKManagedObjectMappingOperationDataSource *dataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:[MGRModel sharedModel].managedObjectStore.persistentStoreManagedObjectContext cache:[MGRModel sharedModel].managedObjectStore.managedObjectCache];
            dataSource.parentOperation = mapper;
            mapper.mappingOperationDataSource = dataSource;
            NSError *mappingError = nil;
            BOOL isMapped = [mapper execute:&mappingError];
            if (isMapped && !mappingError) {
                NSLog(@"jsonMappingResult = %@",[mapper mappingResult]);
                [[[[MGRModel sharedModel] managedObjectStore] persistentStoreManagedObjectContext] save:&mappingError];
            }