I'm writing a utility to asynchronously upload photos from an iOS device to a web server. Each photo is represented by a Photo Core Data entity, which has a property for the photo's filename in the filesystem, and a Boolean has_been_uploaded property.
I want to build a queue of these images to upload, then set their has_been_uploaded property to YES once the upload is complete. I'm using ASIHTTPRequest and ASINetworkQueue for the networking side of things.
Getting the needed images from Core Data and uploading them works just fine, my problem is then updating the has_been_uploaded property for the correct entity.
My code at the moment is this:
for (Photo *myPhoto in [fetchedResultsController fetchedObjects]) {
if(myPhoto.path != nil) {
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://example.com/postimage"]];
NSString *imagePath = [NSString stringWithFormat:@"%@.png",[documentsDirectory stringByAppendingPathComponent:[myPhoto valueForKey:@"path"]]];
[request addFile:imagePath withFileName:[NSString stringWithFormat:@"%@.png",myPhoto.path] andContentType:@"image/png" forKey:@"image"];
[request.userInfo setValue:myPhoto forKey:@"photo"];
[[self imageUploadQueue] addOperation:request];
}
}
[[self imageUploadQueue] go];
But then in the ASIHTTPRequest's didComplete delegate method, the Photo object is always null when I try to retrieve it from the request's userInfo dictionary:
- (void)queueRequestFinished:(ASIHTTPRequest *)request
{
Photo *photo = (Photo *)[request.userInfo valueForKey:@"photo"];
NSLog(@"uploaded image for photo: %@",photo);
}
How can I reliably retrieve the correct Core Data object so that I can update its status once each request has completed?
Thanks!