我在使用RestKit 0.20pre6发布NSManagedObject时遇到问题,导致在我的Rails站点上使用nil值创建一个条目。我怀疑是mapper,但我已经正确地设置了我的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://本地主机:3000 /事件' 2013-01-15 23:25:52.969 fish [38803:16903]我restkit.network:RKHTTPRequestOperation.m:179 POST ' HTTP://本地主机:3000 /事件' (201创建)[0.1121 s] 2013-01-15 23:25:52.971鱼[38803:17507] D. restkit.object_mapping:RKMapperOperation.m:360执行映射 表示的操作:{ event = { airTemp ="&#34 ;; 金额="&#34 ;; " created_at" =" 2013-01-16T04:25:52Z&#34 ;; id = 54; 纬度="&#34 ;; 长度="&#34 ;; 经度="&#34 ;; name ="&#34 ;; thumbnail ="&#34 ;; "的updated_at" =" 2013-01-16T04:25:52Z&#34 ;; " USER_ID" ="&#34 ;; };和targetObject :( entity:Event; id:0x9c7f710 ;数据: { airTemp = 0; 金额=零; createdAt =" 2013-01-16 04:25:52 + 0000&#34 ;; eventId = 0; latitude =" 28.158466&#34 ;; 长度= 0; 经度=" -82.509215&#34 ;; name = nil; 照片=零; thumbnail =零; updatedAt = nil; })2013-01-15 23:25:52.971 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:287找到了可映射的数据 keyPath' event':{ airTemp ="&#34 ;; 金额="&#34 ;; " created_at" =" 2013-01-16T04:25:52Z&#34 ;; id = 54; 纬度="&#34 ;; 长度="&#34 ;; 经度="&#34 ;; name ="&#34 ;; thumbnail ="&#34 ;; "的updated_at" =" 2013-01-16T04:25:52Z&#34 ;; " USER_ID" ="&#34 ;; 2013-01-15 23:25:52.971 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:232问地图来源 对象{ airTemp ="&#34 ;; 金额="&#34 ;; " created_at" =" 2013-01-16T04:25:52Z&#34 ;; id = 54; 纬度="&#34 ;; 长度="&#34 ;; 经度="&#34 ;; name ="&#34 ;; thumbnail ="&#34 ;; "的updated_at" =" 2013-01-16T04:25:52Z&#34 ;; " USER_ID" ="&#34 ;; }使用映射eventId>", "名称>&#34 ;, "量GT;&#34 ;, "长度>&#34 ;, " updatedAt>&#34 ;, " createdAt>&#34 ;, "纬度和GT;&#34 ;, "经度>&#34 ;, " airTemp>&#34 ;, "缩略图>" )> 2013-01-15 23:25:52.971鱼[38803:17507] D. restkit.object_mapping:RKMappingOperation.m:754开始映射 操作... 2013-01-15 23:25:52.972鱼[38803:17507] D. restkit.object_mapping:RKMappingOperation.m:818完成映射 操作成功... 2013-01-15 23:25:52.973鱼[38803:17507] D. restkit.object_mapping:RKMapperOperation.m:316找到不可映射的值 at keyPath:events 2013-01-15 23:25:52.973 fish [38803:17507] D. restkit.object_mapping:RKMapperOperation.m:389完成表演 对象映射。结果:{ event =" (entity:Event; id:0x9c7f710 ;数据: {\ n airTemp = nil; \ n amount = nil; \ n createdAt = \" 2013-01-16 04:25:52 +0000 \"; \ n eventId = 54; \ n纬度= nil; \ n长度 = nil; \ n \ longitude = nil; \ n name = nil; \ n photo = nil; \ n thumbnail = nil; \ n updatedAt = \" 2013-01-16 04:25:52 +0000 \& #34 ;; \ N})&#34 ;; 2013-01-15 23:25:52.973鱼[38803:13403] D. restkit.core_data:RKManagedObjectRequestOperation.m:534跳过 删除孤立对象:仅针对GET请求执行。 2013-01-15 23:25:52.976鱼[38803:11f03] D. restkit.core_data:RKManagedObjectRequestOperation.m:599刷新 映射的目标对象(实体:事件; id: 0x9c7f710 ;数据: { airTemp = nil; 金额=零; createdAt =" 2013-01-16 04:25:52 + 0000&#34 ;; eventId = 54; 纬度=零; 长度=零; 经度=零; name = nil; 照片=零; thumbnail =零; updatedAt =" 2013-01-16 04:25:52 + 0000&#34 ;; })在上下文中
现在这是我的代码,因为我已将所有映射移动到app delegate并实现了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;
答案 0 :(得分:5)
RESTKit 0.20pre6中存在一个错误,它将HTTP Body打印到NSLog,POST请求为(null),即使它是一个对象。我在开发分支中修复了它。 https://github.com/RestKit/RestKit/pull/1139
答案 1 :(得分:2)
对于NSManagedObject
映射,您应使用RKManagedObjectMapping
或RKEntityMapping
,具体取决于您使用的RestKit的版本,而不是RKObjectMapping
。如果您使用的是最后一个,那么它应该是RKEntityMapping
。
NSObject
和NSManagedObject
的指定初始化工具不相同,因此会产生空值。
您可以在他们的维基站点上阅读有关对象映射的更多信息:RestKit Object Mapping,尤其是核心数据部分。
答案 2 :(得分:1)
在更好地理解了managedObjectContexts以及RestKit如何利用其中两个特别之后,我能够进入正确的上下文。使用createContexts创建了两个mainQueue和一个persistentStore Context。同样不是我需要在数据模型中选择我的事件实体并将类从NSManagedObject更改为Event和RestKit然后将该实体识别为类Event的实体。