RestKit 0.20 - using patchObject and passing an image attachment

34 Views Asked by At

I'm performing a patchObject call using the following, based on examples (there are very few) I found:

[[RKObjectManager sharedManager] patchObject:user path:@"profile" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {

    // saving data locally and continuing...    

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];

... but I need also to pass an image. The usual way of adding an image parameter is to add it to a request and then assign this to the operation to be used, i.e.:

request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:user
                                                                       method:RKRequestMethodPOST
                                                                         path:@"profile"
                                                                   parameters:nil
                                                    constructingBodyWithBlock:^(id<AFRKMultipartFormData> formData) {
                                                        [formData appendPartWithFileData:UIImagePNGRepresentation(self.photoImageView.image)
                                                                                    name:@"user[avatar]"
                                                                                fileName:@"photo.png"
                                                                                mimeType:@"image/png"];
                                                    }];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor, errorDescriptor]];
operation.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
operation.managedObjectCache = [RKManagedObjectStore defaultStore].managedObjectCache;
// etc...

As the patchObject method doesn't give me the ability to define the operation (or, therefore, request) being used, I can't see how I can add the image data to the API call.

Previously available methods allowing the image to be defined within a block when performing the patchObject don't seem to exist in RestKit anymore.

Any thoughts? I'd be grateful for any tips on how to do this!

Thanks in advance.

1

There are 1 best solutions below

2
rajat kumar On
+(void)postMutliPartData : (NSString *)urlStr : (NSDictionary *)parameters : (NSData *)data : (void(^)(NSDictionary * response_success))success : (void(^)(NSError * response_error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
[manager POST:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {
     [formData appendPartWithFileData: data name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];

 } progress:nil success:^(NSURLSessionTask *task, id responseObject) {

     NSLog(@"%@",responseObject);
      success(responseObject);

 } failure:^(NSURLSessionTask *operation, NSError *error) {
     failure(error);
 }];    
}