Restkit获取两个表视图控制器的不同数据

时间:2014-06-26 19:58:16

标签: ios objective-c restkit

使用restkit我试图获取两个表视图(收件箱,发送)的数据,表视图控制器的功能相同,除了它们引用不同的web api。 在每个表视图控制器中,我根据表格配置并初始化restkit并请求获取INBOX或SENT数据 一切都很好,但只有一个特定的表,只有首先初始化的表,发生另一个错误

E restkit.network:RKObjectRequestOperation.m:213 GET 'http://myhost/sent/' (200 OK / 0 objects) [request=0.9469s mapping=0.0000s total=0.9963s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x11001a040 {NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://myhost/sent/', which failed to match all (0) response descriptors:, NSErrorFailingURLStringKey=http://myhost/sent/, NSErrorFailingURLKey=http://myhost/sent/, NSUnderlyingError=0x11007f790 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded. 在我的代码中配置restkit:

// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;

RKEntityMapping *sentMapping = [RKEntityMapping mappingForEntityForName:@"Sent" inManagedObjectStore:managedObjectStore];
sentMapping .identificationAttributes = @[ @"message_id" ];
[sentMapping  addAttributeMappingsFromDictionary:@{
                                                       @"objectId":@"message_id",
                                                       @"ToUserName":@"email_to",
                                                       @"createdAt":@"date_send",
                                                       @"Read":@"read"
                                                       }];
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:sentMapping
                                             method:RKRequestMethodGET
                                        pathPattern:@"/sent/"
                                            keyPath:@"results"
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objectManager addResponseDescriptor:responseDescriptor];

我做错了什么?

2 个答案:

答案 0 :(得分:1)

  

无法匹配所有(0)响应描述符

您尚未配置任何响应描述符,或者至少没有任何响应描述符与请求URL路径模式(可能是sent/)和方法(GET)匹配。

检查描述符注册和过滤标准(路径模式,方法和密钥路径)。

答案 1 :(得分:0)

解决! 这很简单!

首先,需要在例如AppDelegate

中初始化RESTKit
    NSURL *baseURL = [NSURL URLWithString:@"http://api.myhost"];
    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    objectManager.managedObjectStore = managedObjectStore;
    [RKObjectManager setSharedManager:objectManager];
............

然后您可以根据需要使用RESTKit 作为示例调用视图控制器:

RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *sentMapping = [RKEntityMapping mappingForEntityForName:@"Sent" inManagedObjectStore:managedObjectStore];
sentMapping.identificationAttributes = @[ @"message_id" ];
[sentMapping  addAttributeMappingsFromDictionary:@{
                                                   @"objectId"  :@"message_id",
                                                   @"ToUserName":@"email_to",
                                                   @"createdAt" :@"date_send",
                                                   @"Subject"   :@"subject",
                                                   @"Message"   :@"message",
                                                   }];
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:sentMapping
                                             method:RKRequestMethodGET
                                        pathPattern:@"/sent/"
                                            keyPath:@"results"
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
...............

并根据需要进一步使用RESTKit