RestKit:如何发布一组对象?
问题摘要:
考虑一个与 KVC 兼容的类 SyncObject
,其属性如下:time
、someValue
、<代码>lastChange,uuid
。
考虑一个仅包含 SyncObject
实例的 NSArray
。
我需要将数组作为 JSON 数组提交到服务器。
如何使用 RestKit 使用 HTTP POST 将此数组提交到服务器?
示例数组:
[
{
"time": "14:45 10/21/2011",
"someValue": "15",
"lastChange": "14:45 10/21/2011",
"uuid": "0b07c510-f4c8-11e0-be50-0800200c9a66"
},
{
"time": "14:50 10/21/2011",
"someValue": "62",
"lastChange": "14:51 10/21/2011",
"uuid": "1a6d4480-f4c8-11e0-be50-0800200c9a66"
}
]
详细信息
我有一个对象数组,我需要将其作为 JSON 发送到服务器。在我看来,RestKit 是最简单的方法:我试图避免将对象转换为一组 NSDictionary
对象,然后使用一些 JSON 编码器来获取 JSON,我可以 POST
到服务器。
因此,创建数组并设置数组中存储的对象类的映射后,我自然会尝试 POST
到服务器。
RKObjectManager* mgr = [RKObjectManager objectManagerWithBaseURL:@"http://localhost/someweb/api/"];
mgr.serializationMIMEType = RKMIMETypeFormURLEncoded;
mgr.client.username = @"username";
mgr.client.password = @"password";
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapKeyPath: @"time" toAttribute:@"time" ];
[mapping mapKeyPath: @"someValue" toAttribute:@"someValue" ];
[mapping mapKeyPath: @"lastChange" toAttribute:@"lastChange" ];
[mapping mapKeyPath: @"uuid" toAttribute:@"uuid" ];
RKObjectMapping* mappingForSerialization = [mapping inverseMapping];
[mgr.mappingProvider setSerializationMapping:mappingForSerialization
forClass:[NSManagedObject class]];
[mgr.router routeClass:[NSManagedObject class] toResourcePath:@"/sync" forMethod:RKRequestMethodPOST];
[mgr postObject:array delegate:nil/*self*/];
然而,这就是我得到的结果:
2011-10-11 14:57:51.769 AppConnect[1974:6e0b] *** Terminating app due to uncaught exception '(null)', reason: 'Unable to find a routable path for object of type '__NSArrayI' for HTTP Method 'POST''
显然,RestKit 不知道如何处理 NSArray。
如何使用 RestKit 发布一组对象?
我尝试了不同的方法:我用通过 RKObjectLoader
手动发送替换了最后一行。
//[mgr postObject:[NSMutableDictionary dictionaryWithObject:array forKey:@"data"] delegate:nil/*self*/];
NSString* syncPath = @"/sync";
RKObjectLoader * objectLoader = [mgr objectLoaderWithResourcePath:syncPath delegate:self];
objectLoader.serializationMIMEType = RKMIMETypeJSON;
objectLoader.method = RKRequestMethodPOST;
//objectLoader.objectClass = [NSManagedObject class];
//objectLoader.managedObjectStore = mgr.objectStore;
objectLoader.params = [NSDictionary dictionaryWithObject:array
forKey:@"MyData"];
[objectLoader send];
不幸的是,这不应用任何类型的映射,而是传输对象描述的数组。设置serializationMIMEType也不会影响传输内容的结构,并且params始终以application/x-www-form-urlencoded的形式传输。
我还尝试分配序列化映射并将对象作为 targetObject
和 sourceObject
传递(这似乎是 RestKit 在 -[RKObjectManager postObject:delegate:]< 内部所做的事情/代码>)。
RKObjectLoader * objectLoader = [mgr objectLoaderWithResourcePath:syncPath delegate:self];
objectLoader.method = RKRequestMethodPOST;
//objectLoader.objectClass = [NSManagedObject class];
//objectLoader.managedObjectStore = mgr.objectStore;
objectLoader.params = [NSMutableDictionary dictionaryWithObject:array
forKey:@"MyData"];
objectLoader.serializationMapping = mapping;
objectLoader.serializationMIMEType = RKMIMETypeJSON;
objectLoader.sourceObject = objectLoader.params;
objectLoader.targetObject = objectLoader.params;
[objectLoader send];
不幸的是,这样不会发生映射:
2011-10-12 12:36:48.143 MyProject[5119:207] D restkit.network:RKObjectLoader.m:290 POST or PUT request for source object {
MyData = (
"<NSManagedObject: 0x5935430> (entity: SomeRecord; id: 0x5934da0 <x-coredata://64DF9977-DA50-4FCD-8C20-4132E58439BF/SomeRecord/p1> ; data: <fault>)",
"<NSManagedObject: 0x5935730> (entity: SomeRecord; id: 0x5934db0 <x-coredata://64DF9977-DA50-4FCD-8C20-4132E58439BF/SomeRecord/p2> ; data: <fault>)"
);
}, serializing to MIME Type application/json for transport...
2011-10-12 12:36:48.143 MyProject[5119:207] D restkit.object_mapping:RKObjectMappingOperation.m:428 Starting mapping operation...
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'time'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'someValue'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'lastChange'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'uuid'
2011-10-12 12:36:48.145 MyProject[5119:207] D restkit.object_mapping:RKObjectMappingOperation.m:448 Mapping operation did not find any mappable content
2011-10-12 12:36:48.146 MyProject[5119:207] T restkit.network:RKRequest.m:211 Prepared POST URLRequest '<NSMutableURLRequest http://someurl/api/sync?request=provide_key>'. HTTP Headers: {
Accept = "application/json";
"Content-Length" = 0;
}. HTTP Body: .
Question summary:
Consider a class SyncObject
that is KVC-compliant with properties such as: time
, someValue
, lastChange
, uuid
.
Consider an NSArray
containing exclusively instances of SyncObject
.
I need to submit the array to the server as a JSON array.
How would one submit this array to the server using HTTP POST using RestKit?
Example array:
[
{
"time": "14:45 10/21/2011",
"someValue": "15",
"lastChange": "14:45 10/21/2011",
"uuid": "0b07c510-f4c8-11e0-be50-0800200c9a66"
},
{
"time": "14:50 10/21/2011",
"someValue": "62",
"lastChange": "14:51 10/21/2011",
"uuid": "1a6d4480-f4c8-11e0-be50-0800200c9a66"
}
]
Details
I have an array of objects that I need to the server as JSON. It seems to me that RestKit is the easiest way to do this: I'm trying to avoid converting objects into a set of NSDictionary
objects, and then using some JSON encoder to get JSON which I can POST
to the server.
So, having created the array, and having set up the mapping for the class of objects stored in the array, I naturally try to POST
to the server.
RKObjectManager* mgr = [RKObjectManager objectManagerWithBaseURL:@"http://localhost/someweb/api/"];
mgr.serializationMIMEType = RKMIMETypeFormURLEncoded;
mgr.client.username = @"username";
mgr.client.password = @"password";
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapKeyPath: @"time" toAttribute:@"time" ];
[mapping mapKeyPath: @"someValue" toAttribute:@"someValue" ];
[mapping mapKeyPath: @"lastChange" toAttribute:@"lastChange" ];
[mapping mapKeyPath: @"uuid" toAttribute:@"uuid" ];
RKObjectMapping* mappingForSerialization = [mapping inverseMapping];
[mgr.mappingProvider setSerializationMapping:mappingForSerialization
forClass:[NSManagedObject class]];
[mgr.router routeClass:[NSManagedObject class] toResourcePath:@"/sync" forMethod:RKRequestMethodPOST];
[mgr postObject:array delegate:nil/*self*/];
However, this is what I get out:
2011-10-11 14:57:51.769 AppConnect[1974:6e0b] *** Terminating app due to uncaught exception '(null)', reason: 'Unable to find a routable path for object of type '__NSArrayI' for HTTP Method 'POST''
Apparently, RestKit does not know how to handle NSArray
s.
How does one post an array of objects using RestKit?
I've tried something different: I replaced the last line with a manual send through RKObjectLoader
.
//[mgr postObject:[NSMutableDictionary dictionaryWithObject:array forKey:@"data"] delegate:nil/*self*/];
NSString* syncPath = @"/sync";
RKObjectLoader * objectLoader = [mgr objectLoaderWithResourcePath:syncPath delegate:self];
objectLoader.serializationMIMEType = RKMIMETypeJSON;
objectLoader.method = RKRequestMethodPOST;
//objectLoader.objectClass = [NSManagedObject class];
//objectLoader.managedObjectStore = mgr.objectStore;
objectLoader.params = [NSDictionary dictionaryWithObject:array
forKey:@"MyData"];
[objectLoader send];
Unfortunately, this does not apply mapping of any sort, and instead transmits an array of objects' descriptions. Setting serializationMIMEType
also does not affect the structure of transmitted contents, and params
are always transmitted as application/x-www-form-urlencoded
.
I also tried assigning serialization mapping and passing the object as targetObject
and sourceObject
(this seems to be what RestKit does internally in -[RKObjectManager postObject:delegate:]
).
RKObjectLoader * objectLoader = [mgr objectLoaderWithResourcePath:syncPath delegate:self];
objectLoader.method = RKRequestMethodPOST;
//objectLoader.objectClass = [NSManagedObject class];
//objectLoader.managedObjectStore = mgr.objectStore;
objectLoader.params = [NSMutableDictionary dictionaryWithObject:array
forKey:@"MyData"];
objectLoader.serializationMapping = mapping;
objectLoader.serializationMIMEType = RKMIMETypeJSON;
objectLoader.sourceObject = objectLoader.params;
objectLoader.targetObject = objectLoader.params;
[objectLoader send];
Unfortunately, no mapping occurs this way:
2011-10-12 12:36:48.143 MyProject[5119:207] D restkit.network:RKObjectLoader.m:290 POST or PUT request for source object {
MyData = (
"<NSManagedObject: 0x5935430> (entity: SomeRecord; id: 0x5934da0 <x-coredata://64DF9977-DA50-4FCD-8C20-4132E58439BF/SomeRecord/p1> ; data: <fault>)",
"<NSManagedObject: 0x5935730> (entity: SomeRecord; id: 0x5934db0 <x-coredata://64DF9977-DA50-4FCD-8C20-4132E58439BF/SomeRecord/p2> ; data: <fault>)"
);
}, serializing to MIME Type application/json for transport...
2011-10-12 12:36:48.143 MyProject[5119:207] D restkit.object_mapping:RKObjectMappingOperation.m:428 Starting mapping operation...
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'time'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'someValue'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'lastChange'
2011-10-12 12:36:48.145 MyProject[5119:207] T restkit.object_mapping:RKObjectMappingOperation.m:291 Did not find mappable attribute value keyPath 'uuid'
2011-10-12 12:36:48.145 MyProject[5119:207] D restkit.object_mapping:RKObjectMappingOperation.m:448 Mapping operation did not find any mappable content
2011-10-12 12:36:48.146 MyProject[5119:207] T restkit.network:RKRequest.m:211 Prepared POST URLRequest '<NSMutableURLRequest http://someurl/api/sync?request=provide_key>'. HTTP Headers: {
Accept = "application/json";
"Content-Length" = 0;
}. HTTP Body: .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Restkit 不为 NSArray 提供可路由路径,因为您为
NSManagedObject
类定义了路由。您可能想要创建一个自定义类,例如MySyncEntity
,它保存您在映射中定义的 ivars。然后,您可以像这样创建映射:然后您应该能够将对象作为 JSON 对象发布到 API 后端。
进一步说明:
在本例中,我们希望将一组
NSManagedObject
实例发布到基于 JSON 的 API 中。为此,我们需要创建一个同步实体,将对象保存在数组中:mySyncArray
将保存我们想要提交到其余后端的有效负载。然后,我们为将在mySyncArray
中发送的NSManagedObject
和MySyncEntity
实体本身创建适当的映射。现在,定义映射后,我们可以将对象发布到服务器。
mySyncInstance
数组的内容将根据mngObjMapping
进行映射,并发送到定义的其余端点。The restkit does not fund routable path for NSArray, because you defined your routing for
NSManagedObject
class. You probably want to create a custom class, sayMySyncEntity
that holds the ivars you define in your mapping. Then, you create your mapping like this:then you should be able to post your object to the API backend as JSON object.
Further clarification:
In this case, we want to post an array of
NSManagedObject
instances into a JSON based API. To do that we need to create a sync entity, that holds the objects in an array:The
mySyncArray
will hold the payload we'd like to submit to the rest backend. Then, we create appropriate mapping for bothNSManagedObject
that will be sent inmySyncArray
and theMySyncEntity
entity itself.Now with the mappings defined we can post the object to the server
The contents of
mySyncInstance
array will be mapped according tomngObjMapping
and sent to defined rest endpoint.作为进一步的澄清,我想指出 mja 的答案 关键是
这说“
mySyncArray
keypath 是一个数组,其中包含应根据mngObjMapping
”。As a further clarification, I'd like to point out that in mja's answer the key thing is
This says "the
mySyncArray
keypath is an array which contains objects that should be mapped according tomngObjMapping
".