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);
}];
除NSPredict和过滤数据外,是否可以在映射时手动插入值(如字符串)?
答案 0 :(得分:6)
您可以在完成块中修改映射结果中的对象,但是您需要显式保存上下文,并且上下文的其他观察者将收到保存通知。这是一种超级简单的方法。
或者,您可以覆盖willSave
或使用NSManagedObjectContextWillSaveNotification
(后者是更好的选项)来触发您的自定义逻辑。然后,您的更改将与RestKit更改一起内联,并将自动保存。
答案 1 :(得分:2)
使用
解决了这个问题[[mappingResult set] setValue:value forKey:key];
在成功经理的经理身上。