【问题标题】:Restkit getting different data for two table view controllersRestkit 为两个表视图控制器获取不同的数据
【发布时间】:2014-06-26 19:58:15
【问题描述】:

我正在尝试使用 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];

我做错了什么?

【问题讨论】:

  • 我认为这与您的 tableview 没有太大关系。可能与映射有关。
  • @YarGnawh 为第二种情况正确初始化 restkit?

标签: ios objective-c restkit


【解决方案1】:

未能匹配所有 (0) 个响应描述符

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

检查描述符注册和过滤条件(路径模式、方法和关键路径)。

【讨论】:

  • 感谢您的快速响应。但在我的情况下,这完全取决于我首先打开哪种表视图,然后应该没有错误地完成,但是当我打开另一个表视图时会发生此错误,我认为我的问题是正确初始化restkit这两种不同的情况
  • 您是否在代码中两次调用[[RKObjectManager alloc] init... 并在任何地方使用sharedManager
  • 当然!我想我开始明白我的错误了
  • 我应该只调用一次吗,例如在 appDelegate 中?
  • 是一次,但不是在应用程序委托中(出生两次,并在属性中保留引用)。
【解决方案2】:

解决了! 很简单!

首先,需要在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多