RESTKit:在搜索的关键路径中找不到可映射的对象表示
Posted
技术标签:
【中文标题】RESTKit:在搜索的关键路径中找不到可映射的对象表示【英文标题】:RESTKit: No mappable object representations were found at the key paths searched 【发布时间】:2014-04-09 22:20:12 【问题描述】:日志:
2014-04-09 15:03:10.196 App[49808:60b] request <NSMutableURLRequest: 0xd091170> URL: http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z
2014-04-09 15:03:10.214 App[49808:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z'
2014-04-09 15:03:10.300 App[49808:632f] D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation:
meetups = (
activities = (
activityId = 37;
activityType = "<null>";
customData = "<null>";
locationAddress = "k2-LocationAddress";
locationName = "k2-LocationName";
startTime = "0000-00-00 00:00:00";
,
activityId = 38;
activityType = "<null>";
customData = "<null>";
locationAddress = "k-LocationAddress";
locationName = "k-LocationName";
startTime = "0000-00-00 00:00:00";
);
comment = comment;
gettingBack = "gettingBack Test";
gettingThere = "gettingBack Test";
meetingType = 1;
meetupId = 65;
modified = "2014-04-09T20:59:29.000Z";
ownerId = Joe;
recipientId = "<null>";
);
and targetObject: (null)
2014-04-09 15:03:10.301 App[49808:632f] D restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: (null)
2014-04-09 15:03:10.302 App[49808:21f] E restkit.network:RKObjectRequestOperation.m:243 GET 'http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z' (200 OK / 0 objects) [request=0.0849s mapping=0.0000s total=0.1043s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0xd0b5a40 NSErrorFailingURLStringKey=http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z, NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z', which failed to match all (0) response descriptors:, NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=http://www.domain.com/meetups?lastrequest=2014-04-09T15%3A03%3A10Z, NSUnderlyingError=0xd08a190 "No mappable object representations were found at the key paths searched."
代码:
@property (strong, nonatomic) RKObjectManager *objectManager;
....
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.objectManager = [self getObjectManager];
self.objectManager.managedObjectStore = [appDelegate setupCoreDataWithRESTKit];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:@"Invite" inManagedObjectStore:self.objectManager.managedObjectStore];
[invitationMapping addAttributeMappingsFromDictionary:@
@"recipientId" : @"recipientId",
@"meetingType" : @"meetingType",
@"startDate" : @"startDate",
@"gettingThere" : @"gettingThere",
@"gettingBack" : @"gettingBack",
@"comment" : @"comment"
];
invitationMapping.identificationAttributes = @[@"meetupId"];
RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:@"Activity" inManagedObjectStore:self.objectManager.managedObjectStore];
[activityMapping addAttributeMappingsFromDictionary:@
@"locationName" : @"locationName",
@"locationAddress" : @"locationAddress",
@"startTime" : @"startTime",
@"customData" : @"customData"
];
activityMapping.identificationAttributes = @[@"activityId"];
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
[invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"activities" toKeyPath:@"activities" withMapping:activityMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:invitationMapping
method:RKRequestMethodGET
pathPattern:@"/meetups"
keyPath:@"meetups" statusCodes:statusCodeSet];
NSMutableURLRequest *request = [self.objectManager.HTTPClient requestWithMethod:@"GET"
path:@"/meetups"
parameters:@@"lastrequest": dateString
];
[self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation = [self.objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:self.objectManager.managedObjectStore.mainQueueManagedObjectContext success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSArray *array = mappingResult.array;
NSLog(@"array %@", array);
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
[operation start];
Invite
与Activity
有一对多关系,称为activities
。
为什么我得到:“在搜索的关键路径中找不到可映射的对象表示。” ?
【问题讨论】:
开启跟踪日志。将响应描述符上的路径模式设置为 nil(仅在直接使用RKManagedObjectRequestOperation
时)。您不直接使用对象管理器是否有原因?
谢谢。我确实在 AppDelegate 中打开了跟踪日志记录:RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);当我将路径模式设置为 nil 时,我得到了同样的错误。我正在使用 RKManagedObjectRequestOperation, b/c 这似乎是基于其他 SO 问题的建议。我应该改用对象管理器吗?
我个人 99% 直接使用RKObjectManager
和 1% 的具体操作。您使用的原始 URL 是什么?
NSURL *baseURL = [NSURL URLWithString:@"rest.domain.com"]; 你需要真实域名吗?
我想知道它最后是否有一个/
,但是直接使用RKManagedObjectRequestOperation
时这并不重要。我看不出有什么特别的问题。您可以在某处发布跟踪日志吗?
【参考方案1】:
这是创建操作的正确方法:
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
但是你用另一个创建的替换那个:
operation = [self.objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:self.objectManager.managedObjectStore.mainQueueManagedObjectContext success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
正确使用对象管理器。这个问题是你没有把响应描述符给对象管理器,你只给了刚刚被销毁的操作(因为你替换了它)。
因此,使用您创建的第一个操作并通过调用setCompletionBlockWithSuccess:failure:
在其上设置成功和失败块。
【讨论】:
您需要在执行操作之前设置managedObjectContext
(使用对象管理器的另一个好处是这是为您完成的)
操作.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;我认为上面的这条线做到了!
最后一个问题:我看到对象返回并被映射。它们是如何保存在 Core Data 中的?
只是一个小问题 :-) RestKit 使用您的responseDescriptor
来了解要创建的实体类型以及如何将传入数据映射到这些新实例中。映射完成后,它会保存 MOC。这就是为什么您需要设置操作的managedObjectContext
。因此它可以创建并保存响应。
我目前不使用 Swift,因为它还不够成熟,我的任何客户都无法使用它。目前(据我所知)没有任何将 RestKit 迁移到 Swift 的计划。我还没有打算离开 RestKit以上是关于RESTKit:在搜索的关键路径中找不到可映射的对象表示的主要内容,如果未能解决你的问题,请参考以下文章
在gnc gcc编译器的已配置搜索路径中找不到文件可执行文件