我在使用 RestKit 0.20pre6 发布 NSManagedObject 时遇到问题,这会导致在我的 Rails 站点上创建一个具有 nil 值的条目。我怀疑映射器,但据我所知,我已经正确设置了我的 RK 对象。例子很少,支持也很有限。该示例处理 NSObjects 而不是 CoreData,因此我不得不做出一些假设。
这是我的发布代码,后面是控制台输出。该应用程序可以很好地从服务器获取对象。
// Sync the added object to the server
// ****************************************
RKObjecMapping *responseMapping = [RKObjectMapping mappingForClass:[Event class]];
// RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:self.managedObjectStore];
[responseMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *eventDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:@"/events"
keyPath:@"events"
statusCodes:statusCodes
];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
// For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
// under the 'article' key path
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Event class]
rootKeyPath:@"events"
];
LocationsAppDelegate *appDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
// RKObjectManager *manager = [RKObjectManager managerWithBaseURL:gBaseURL];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
[[RKObjectManager sharedManager] addResponseDescriptor:eventDescriptor];
NSLog(@"RVC-addEvent %@ - %@", appDelegate.user.login, appDelegate.user.password);
[[NBEventManager sharedManager] setUsername:appDelegate.user.login andPassword:appDelegate.user.password];
appDelegate.objectManager.HTTPClient = [NBEventManager sharedManager];
event = [eventsArray objectAtIndex:0];
[[RKObjectManager sharedManager] postObject:event
path:@"/events"
parameters:nil
success:nil
failure:nil
];
}
2013-01-15 23:25:52.857 鱼[38803:13403] 我 restkit.network:RKHTTPRequestOperation.m:152 POST 'http://localhost:3000/events' 2013-01-15 23:25:52.969 鱼[ 38803:16903] 我 restkit.network:RKHTTPRequestOperation.m:179 POST 'http://localhost:3000/events' (201 Created) [0.1121 s] 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:360 为表示执行映射操作:{ event = { airTemp = ""; 金额 = ""; "created_at" = "2013-01-16T04:25:52Z"; ID = 54; 纬度 = ""; 长度 = ""; 经度 = ""; 名称 = ""; 缩略图 = ""; "updated_at" = "2013-01-16T04:25:52Z"; “用户ID”=“”;}; } 和目标对象:(实体:事件;id:0x9c7f710;数据:{ airTemp = 0;数量 = nil;createdAt =“2013-01-16 04:25:52 +0000”;eventId = 0;纬度 =“28.158466” ;长度 = 0;经度 =“-82.509215”;名称 = 无;照片 = 无;缩略图 = 无;更新时间 = 无;}) 2013-01-15 23:25:52.971 鱼[38803:17507] D restkit.object_mapping :RKMapperOperation.m:287 在 keyPath 'event' 找到可映射的数据:{ airTemp = ""; 金额 = ""; "created_at" = "2013-01-16T04:25:52Z"; ID = 54; 纬度 = ""; 长度 = ""; 经度 = ""; 名称 = ""; 缩略图 = ""; "updated_at" = "2013-01-16T04:25:52Z"; “用户ID”=“”;} 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:232 要求映射源对象 { airTemp = ""; 金额 = ""; "created_at" = "2013-01-16T04:25:52Z"; ID = 54; 纬度 = ""; 长度 = ""; 经度 = ""; 名称 = ""; 缩略图 = ""; "updated_at" = "2013-01-16T04:25:52Z"; “用户ID”=“”;} 使用映射 eventId>"、"name>"、"amount>"、"length>"、"updatedAt>"、"createdAt>"、"latitude>"、“经度>”、“airTemp>”、“缩略图>”)> 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:754 开始映射操作... 2013- 01-15 23:25:52.972 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:818 成功完成映射操作... 2013-01-15 23:25:52.973 fish[38803:17507] D restkit. object_mapping:RKMapperOperation.m:316 在 keyPath 找到不可映射的值:事件 2013-01-15 23:25:52.973 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:389 完成执行对象映射。结果:{ event = " (entity: Event; id: 0x9c7f710 ; data: {\n airTemp = nil;\n amount = nil;\n createdAt = \"2013-01-16 04:25:52 +0000\" ;\n eventId = 54;\n 纬度 = nil; 13403] D restkit.core_data:RKManagedObjectRequestOperation.m:534 跳过删除孤立对象:仅针对 GET 请求执行。2013-01-15 23:25:52.976 fish[38803:11f03] D restkit.core_data:RKManagedObjectRequestOperation.m:599 刷新映射的目标对象(实体:事件;ID:0x9c7f710;数据:{ airTemp = nil;数量 = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 54; latitude = nil; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt = "2013- 01-16 04:25:52 +0000"; }) 在上下文中 13403] D restkit.core_data:RKManagedObjectRequestOperation.m:534 跳过删除孤立对象:仅针对 GET 请求执行。2013-01-15 23:25:52.976 fish[38803:11f03] D restkit.core_data:RKManagedObjectRequestOperation.m:599 刷新映射的目标对象(实体:事件;ID:0x9c7f710;数据:{ airTemp = nil;数量 = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 54; latitude = nil; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt = "2013- 01-16 04:25:52 +0000"; }) 在上下文中
这是我的代码,因为我已将所有映射移至应用程序委托并实现了 RKEntityMapping。我仍然没有收到附加到 POST 调用的 JSON 对象。
RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
[eventMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
eventMapping.identificationAttributes = @[ @"eventId" ];
//RKRelationshipMapping *eventRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"events"
// toKeyPath:@"events"
// withMapping:eventMapping];
//[eventMapping addPropertyMapping:eventRelationship];
// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping
pathPattern:nil
keyPath:@"event"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
RKEntityMapping *eventRequestMapping = [eventMapping inverseMapping];
RKRequestDescriptor *eventRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:eventRequestMapping
objectClass:[Event class]
rootKeyPath:@"event"
];
[objectManager addRequestDescriptor:eventRequestDescriptor];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;