2

我正在使用 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我设置 的方法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

#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

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

4

2 回答 2

4

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

于 2013-04-24T20:31:47.030 回答
1

由于您正在处理核心数据,因此您需要使用 RKManagedObjectRequestOperation 而不是 RKObjectRequestOperation。

于 2013-04-24T20:27:55.050 回答