Question

I'm using RestKit to post images to a web service and they're posting fine, with the server sending 200 back. What's happening is that I'm getting the following error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "resource_updated_at".'

Here is the method I'm using to post the image.. I'm hoping there's something obvious that I'm missing. I've found a similar post that mentions that core data stack might not be set up correctly, but I'm not sure how. I'm using RestKit to post other standard form data and it returns and maps just fine.

- (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);
            }];
        }
    }
}

Also, here's the init method where I set up the RKManagedObjectStore, in case that's relevant.

- (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;
}

Here is the Image class that represents the 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

I'll put up the disclaimer that this is probably not the best code.. but please let me know if you see THE error :].

Was it helpful?

Solution

I had exactly the same error. You need to use the managed version of RKObjectRequestOperation (RKManagedObjectRequestOperation).

OTHER TIPS

Since you're dealing with Core Data, you need to use a RKManagedObjectRequestOperation instead of RKObjectRequestOperation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top