我正在使用 RestKit 将图像发布到 Web 服务,它们发布得很好,服务器发回了 200。发生的情况是我收到以下错误:
***
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "resource_updated_at".'
这是我用来发布图像的方法..我希望我遗漏了一些明显的东西。我发现了一篇类似的文章,其中提到核心数据堆栈可能未正确设置,但我不确定如何设置。我正在使用 RestKit 发布其他标准表单数据,它返回并映射得很好。
- (void)postImagesFromInventory:(NSManagedObject *)inventory {
NSLog(@"######## postImagesFromInventory ########");
// if there are photos that aren't synced, post the first one
int postingImageNumber = 0;
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]];
// send for each image in inventory
for (Image *image in [[inventory valueForKey:@"image"] allObjects]) {
postingImageNumber = postingImageNumber + 1;
[SVProgressHUD setStatus:[NSString stringWithFormat:@"Posting image %d of %d for inventory %@", postingImageNumber, [[inventory valueForKey:@"image"] allObjects].count, [inventory valueForKey:@"asset_name"]]];
// make sure it's not already synced
if (![image valueForKey:@"synced"]) {
NSLog(@"-------------------------------------");
NSLog(@"need to post image for inventory %@",[inventory valueForKey:@"inventory_id"]);
NSURL *imagePath = [NSURL URLWithString:[image valueForKey:@"path"]];
// need to get my image from Asset Library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSNumber *imageSize = nil;
__block UIImage *myImage = nil;
__block Image *blockImage = nil;
__block NSData *blockImageData = nil;
[library assetForURL:imagePath resultBlock:^(ALAsset *asset) {
myImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
imageSize = [NSNumber numberWithLongLong:asset.defaultRepresentation.size];
// make the image available on callback
blockImage = image;
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
blockImageData = imageData;
NSMutableURLRequest *request = [manager multipartFormRequestWithObject:inventory method:RKRequestMethodPOST path:IMAGES_ENDPOINT parameters:@{@"auth_token":@"my_auth_token",@"image" : @{@"asset_inventory_id":[inventory valueForKey:@"inventory_id"],@"resource_content_type":@"image/jpeg",@"resource_file_name":[NSString stringWithFormat:@"%@_image.jpg",[inventory valueForKey:@"asset_name"]],@"resource_file_size": [NSString stringWithFormat:@"%@",imageSize]}} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"image[resource]"
fileName:[NSString stringWithFormat:@"%@_image.jpg",[inventory valueForKey:@"asset_name"]]
mimeType:@"image/jpeg"];
}];
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Image" inManagedObjectStore:_managedStore];
[mapping addAttributeMappingsFromDictionary:@{@"resource_updated_at":@"resource_updated_at"}];
[mapping mappingForDestinationKeyPath:@"image"];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];
RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"operation successful");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"operation failed");
}];
// queue up the operation
[manager enqueueObjectRequestOperation:operation];
// library access failure
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
}
}
}
另外,这里是我设置 RKManagedObjectStore
的 init
方法,以防相关。
- (PSNDataSync *)init {
RKLogConfigureByName("RestKit/Network*", RKLogLevelDebug);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelDebug);
PSNAppDelegate *appDelegate = (PSNAppDelegate *)[[UIApplication sharedApplication] delegate];
_context = [appDelegate managedObjectContext];
_managedStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[appDelegate managedObjectModel]];
_currentInventory = nil;
_imagesArray = nil;
// initialize imagesArray for loading inventoryCollectionView
_imagesArray = [[NSMutableArray alloc]init];
return self;
}
这是代表核心数据实体
的图像类
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Inventory;
@interface Image : NSManagedObject
@property (nonatomic, retain) NSString * path;
@property (nonatomic, retain) NSString * resource_updated_at;
@property (nonatomic, retain) NSNumber * synced;
@property (nonatomic, retain) Inventory *inventory;
@end
我将提出免责声明,这可能不是最好的代码..但如果您看到错误,请告诉我:]。
最佳答案
我遇到了完全相同的错误。您需要使用 RKObjectRequestOperation 的托管版本(RKManagedObjectRequestOperation)。
关于objective-c - RestKit .2 将图像发布到 Web 服务 - 空实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16175547/