RestKit .2将图像发布到Web服务 - 空实体错误

时间:2013-04-23 17:28:56

标签: objective-c ios6 restkit restkit-0.20

我正在使用RestKit将图像发布到Web服务,并且它们发布正常,服务器发回200。发生的事情是我收到以下错误:

  

***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:实体(null)与密钥“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);
            }];
        }
    }
}

此外,这是我设置init的{​​{1}}方法,如果相关的话。

RKManagedObjectStore

以下是代表- (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; }

Image class
Core Data Entity

我会提出免责声明,这可能不是最好的代码..但如果你看到错误,请告诉我:]。

2 个答案:

答案 0 :(得分:4)

我有完全相同的错误。您需要使用RKObjectRequestOperation的托管版本(RKManagedObjectRequestOperation)。

答案 1 :(得分:1)

由于您正在处理Core Data,因此需要使用RKManagedObjectRequestOperation而不是RKObjectRequestOperation。