我有一个RKEntitiyMapping对象,用于映射与子项具有一对一关系的对象。
调用以下代码时:
[sessionMapping addConnectionForRelationship:@"dailyGoalDetail" connectedBy:@{ @"dailyGoalDetailId": @"dailyGoalDetailId" }];
通过addConnectionForRelationship方法进行调试,我发现添加到连接集合的连接是nil。 我通过这种方式找到与Core Data的连接进行了双重检查:
NSEntityDescription *parentEntity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:[MGRModel sharedModel].managedObjectStore.persistentStoreManagedObjectContext];
NSRelationshipDescription *childRelationship = [parentEntity relationshipsByName][@"dailyGoalDetail"];
[sessionMapping addConnectionForRelationship:childRelationship connectedBy:@{@"dailyGoalDetailId":@"dailyGoalDetailId"}];
登录到控制台我的关系显示它。但是addConnectionForRelationship方法仍然会为其集合添加一个nil连接。
这有什么问题?
更新
这是我的Session对象的相关代码:
+ (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;
}
这是关系中的一个方面:
(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
更多信息。 JSON是这样的:
{
"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"
},
]
}
]
}
初始化并调用mapper的相关代码(这是通过NSString完成的):
// 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];
}
答案 0 :(得分:0)
RKMapperOperation
实际上并没有做太多工作。在内部,它使用RKMappingOperation
,这就是连接的原因。为了使它工作,你有两个选择:
RKMapperOperation
个实例,会话映射器运行第二个RKObjectManager
监督整个过程如果您直接使用mapper操作,那么您将需要其中两个,以便您可以明确控制订单。从理论上讲,您也可以将连接添加到两个映射中,因此首先运行哪个并不重要(只要它们都由相同的MOC支持)。