【问题标题】:Need to Create Multiple Identical Entities using RestKit需要使用 RestKit 创建多个相同的实体
【发布时间】:2014-08-11 17:23:19
【问题描述】:

我在 RESTKit 中对不同 URL 的多次调用通常会下载相同的数据。第一次调用映射良好,但随后的调用会替换第一次调用的实体。

我想要的行为是在其父实体中拥有唯一的对象,因此即使数据看起来相同,我仍然希望创建一个新对象,但目前看起来 RESTKit 希望它们成为在整个数据库中是唯一的。在我正在下载的数据中没有唯一的键来执行此操作,对象实际上是相同的。下面是我用来创建操作的代码。如何设置它以允许重复?

NSMutableURLRequest *request = [self requestWithURL:URL];
[request setHTTPMethod:@"GET"];

RKHTTPRequestOperation *requestOperation = [[RKHTTPRequestOperation alloc]initWithRequest:request];

RKResponseDescriptor *responseDescriptor = [INKResponseDescriptorFactory journeySegmentDescriptorForPath:URL.path inStore:[RKObjectManager sharedManager].managedObjectStore];

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithHTTPRequestOperation:requestOperation
                                                                                               responseDescriptors:@[responseDescriptor]];

operation.managedObjectContext = [self.objectManager context];
operation.managedObjectCache = self.objectManager.managedObjectStore.managedObjectCache;
operation.savesToPersistentStore = NO;

[operation setCompletionBlockWithSuccess: ^(RKObjectRequestOperation *requestOperation, RKMappingResult *mappingResult)
{   
    success();
} failure: ^(RKObjectRequestOperation *requestOperation, NSError *error) {
    failure(error);
}];

[self.objectManager enqueueObjectRequestOperation:operation];

【问题讨论】:

    标签: core-data restkit nsmanagedobject


    【解决方案1】:

    想出了一个办法。

    每次我更新数据时,即使我获得的实体包含相同的数据,我也会从不同的 url 检索它们。

    RESTKit 允许您从调用中检索元数据并将其映射到您的托管对象属性中。

    所以我所做的是将用于请求的 URL 映射到一个属性中,并使用该 URL 以及一个标识符,该标识符在此调用返回的对象中是唯一的,以使对象在整个数据库中都是唯一的。

    所以我的映射现在看起来像这样:

    RKEntityMapping *seatMapping = [RKEntityMapping mappingForEntityForName:@"Seat" inManagedObjectStore:store];
    [seatMapping addAttributeMappingsFromDictionary:@{ @"designator" : @"designator",
                                                       @"status" : @"status",
                                                       @"@metadata.HTTP.request.URL" : @"requestURL"}];
    
    seatMapping.identificationAttributes = @[@"requestURL", @"designator"]; 
    

    【讨论】:

    • 另一种方法是删除operation.managedObjectCache = self.objectManager.managedObjectStore.managedObjectCache;,但是您需要处理任何意外重复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2018-07-07
    • 2010-10-12
    • 2015-06-10
    • 1970-01-01
    • 2021-08-01
    • 2021-05-28
    相关资源
    最近更新 更多