简体   繁体   English

restkit 0.20:不能将NSManagedObject传递给getObject:方法

[英]restkit 0.20: can not pass in an NSManagedObject to the getObject: method

I am simply trying to retrieve one item from my restful service (confirmed working) 我只是想从我的静态服务中检索一项(已确认工作)

http://www.example.com/person/25

I need to somehow pass in the '25' part of the URL. 我需要以某种方式传递URL的“ 25”部分。 I figure that I'm supposed to use RKRoute. 我认为我应该使用RKRoute。 The way to execute this in RestKit is to use the getObject: method of RKObjectManager. 在RestKit中执行此操作的方法是使用RKObjectManager的getObject:方法。

Using RestKit 0.22 and I do not know how to call the getObject: method because it expects a full Person object, no? 使用RestKit 0.22,并且我不知道如何调用getObject:方法,因为它需要完整的Person对象,不是吗? Even though I only need to pass in the string '25'. 即使我只需要传递字符串“ 25”。

The method in question is: [self getObject:personNSManagedObject path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) ..... 有问题的方法是: [self getObject:personNSManagedObject path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) .....

I can't just say "Person *person = [[Person alloc] init] because Person is an NSManagedObject. 我不能只说“ Person * person = [[Person alloc] init],因为Person是一个NSManagedObject。

But when I do this: 但是当我这样做时:

NSManagedObjectContext *moc = self.managedObjectStore.mainQueueManagedObjectContext;
NSEntityDescription *person = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:moc];
Person *personNSManObj = [[Person alloc] initWithEntity:person insertIntoManagedObjectContext:nil];
personNSManObj.id = 3;

I get the following error: 我收到以下错误:

Performing object mapping to temporary target objectID. 执行对象映射到临时目标objectID。 Results may not be accessible without obtaining a permanent object ID. 如果不获取永久对象ID,可能无法访问结果。 restkit.network:RKResponseMapperOperation.m:457 Failed to retrieve existing object with ID: 0x1664ac90 restkit.network:RKResponseMapperOperation.m:457无法检索ID为0x1664ac90的现有对象

Any ideas? 有任何想法吗?

You should save the new object back to the persistent store. 您应该将新对象保存回持久性存储。 The data load and mapping is happening on a different thread but will try to map data back into the supplied object. 数据加载和映射是在另一个线程上进行的,但是会尝试将数据映射回提供的对象。 Because the object is inserted but not saved it has a temporary managed object id which RestKit tries to convert into an object but can't. 因为对象已插入但未保存,所以它具有一个临时的受管对象ID,RestKit尝试将其转换为对象,但不能。 Saving will create a 'stub' object in the data store which RestKit will fully populate with the response data (assuming success). 保存将在数据存储区中创建一个“存根”对象,RestKit将使用响应数据完全填充该存根对象(假设成功)。

An alternative approach is to supply a dictionary with the required key / value pair which will be inserted into the route. 一种替代方法是为字典提供所需的键/值对,该键/值对将插入到路由中。 RestKit will then create the destination object for you. 然后,RestKit将为您创建目标对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM