Restkit在映射时添加自定义值

A7m*_*dev 4 core-data ios restkit

Restkit映射和插入数据工作正常,但我需要向数据库添加自定义值(而不是从JSON)

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:entityName inManagedObjectStore:managedObjectStore];

[entityMapping addAttributeMappingsFromDictionary:dict];

if (uniqKey != nil) {
    entityMapping.identificationAttributes = @[ uniqKey ];
}

// Set MIME Type to JSON
manager.requestSerializationMIMEType = RKMIMETypeJSON;

// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:entityMapping
                                             method:RKRequestMethodPOST
                                        pathPattern:path
                                            keyPath:rootKeyPath
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

[manager addResponseDescriptor:responseDescriptor];

[manager postObject:nil path:path parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (mappingResult.array.count != 0) {
    NSDictionary *data = mappingResult.array[0];       
    NSLog(@"data: %@", data);      
}else{
    NSLog(@"Unable to fetch data from: %@", path);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error response': %@", error);
}];
Run Code Online (Sandbox Code Playgroud)

除了NSPredict和过滤数据之外,是否可以在映射时手动插入值(如字符串)?

Wai*_*ain 6

您可以在完成块中修改映射结果中的对象,但是您需要显式保存上下文,并且上下文的其他观察者将收到保存通知.这是超级简单的方法.

或者,您可以覆盖willSave或使用NSManagedObjectContextWillSaveNotification(后者是更好的选项)来触发您的自定义逻辑.然后,您的更改将与RestKit更改一起内联,并将自动保存.