IPhone: File Upload using dispatch_async for background upload - iphone

I want to perform a image upload while the running application at the background. I am able to upload the image to the server using the code on this link.
How can I upload a photo to a server with the iPhone?
I heard the NSUrlConnection can be asynchronous and it was used in the EPUploader. In my code, I add some extra method that will create a file in the application directory used for the EPUploader. During this creation of the file, I don't want it to create on the main thread of the application so I wrap all the code including the EPUploader itself with the
dispatch_async on the global queue. That way I won't block the main thread while the file are creating.
It has no problem if I use dispatch_sync but dispatch_async I find something weird when I placed the breakpoint at NSUrlConnection connection :
- (void)upload
{
NSData *data = [NSData dataWithContentsOfFile:filePath];
//ASSERT(data);
if (!data) {
[self uploadSucceeded:NO];
return;
}
if ([data length] == 0) {
// There's no data, treat this the same as no file.
[self uploadSucceeded:YES];
return;
} /* blah blah */
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (!connection) {
[self uploadSucceeded:NO];
return;
}
else
return;
I went to debug at the breakpoint and instead of going to if statement, the debugger jumps to the first return statement of this method. After that, the selectors I passed on to this class never gets called. This happen only on dispatch_async and it work on dispatch_sync on the global queue.
Does anybody know how to solve this issue?
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
self.uploadIndex = 0;
ALAsset *asset = [self.assets objectAtIndex:0];
[[FileUploader alloc] initWithAsset:[NSURL URLWithString:#"http://192.168.0.3:4159/default.aspx"]
asset:asset
delegate:self
doneSelector:#selector(onUploadDone:)
errorSelector:#selector(onUploadError:)];
//[self singleUpload:self.uploadIndex];
[pool release];
});

There are a couple of things that should be changed.
Remove the NSAutoreleasePool, it is not needed.
Copy the block to the heap because it's life will probably exceed that of the calling code.
Example:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, [[^{
self.uploadIndex = 0;
ALAsset *asset = [self.assets objectAtIndex:0];
[[FileUploader alloc] initWithAsset:[NSURL URLWithString:#"http://192.168.0.3:4159/default.aspx"]
asset:asset
delegate:self
doneSelector:#selector(onUploadDone:)
errorSelector:#selector(onUploadError:)];
} copy] autorelease]);
If you are using ARC (which you certainly are since you should be) there is no need for the copy or autorelease.

Related

How to run a thread in background of application which will not affect my application user interface

I have made an application for IOS, in which I am using sqlite database, database is local in application.
Now I have given the functionality the application is downloading data from internet & put it in local database & show infront of user. I have given this functionality on - (void)viewDidLoad so that when application is downloading data, it stop working till it finish downloading part, for this user need to wait to interact with application.
Now I wants to functionality a thread run in background of application which will connect the internet & update the application without interfering user.
please help me.
My code of download & save image is this:
-(void)Download_save_images:(NSString *)imagesURLPath :(NSString *)image_name
{
NSMutableString *theString = [NSMutableString string];
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:NSURL URLWithString:imagesURLPath]]];
NSLog(#"%f,%f",image.size.width,image.size.height);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// If you go to the folder below, you will find those pictures
NSLog(#"%#",docDir);
[theString appendString:#"%#/"];
[theString appendString:image_name];
NSLog(#"%#",theString);
NSLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:theString,docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
NSLog(#"saving image done");
[image release];
// [theString release];
}
when i am debugging application i seen my application taking more time at below line:
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:NSURL URLWithString:imagesURLPath]]];
You can also use NSOperationQueue and NSBlockOperation if you find GCD difficult.
NSBlockOperation *operation=[[NSBlockOperation alloc] init];
[operation addExecutionBlock:^{
//Your code goes here
}];
NSOperationQueue *queue=[[NSOperationQueue alloc] init];
[queue addOperation:operation];
In GCD You could achieve the same using
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//Your Code to download goes here
dispatch_async(dispatch_get_main_queue(), ^{
//your code to update UI if any goes here
});
});
Use either of the API Depending upon your needs. Check this thread which discuss about NSOperationQueue vs GCD for more info.
Questions like these were asked hundreds of times before. I suggest you do a quick search on this. Also there is a topic in apple documentation covering this area thoroughly. here
Basically you can do this with operation queues or dispatch queues. There are some code snippets given above by Avi & Amar.
I'd like to add something since you seem to be unfamiliar with the topic & you mentioned there are web requesting involved.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Dont DIRECTLY request your NSURL Connection in this part because it will never return data to the delegate...
// Remember your network requests like NSURLConnections must be invoked in mainqueue
// So what ever method you're invoking for NSURLConnection it should be on the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// this will run in main thread. update your UI here.
});
});
I've given small example below. you can generalise the idea to match your needs.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Do your background work here
// Your network request must be on main queue. this can be raw code like this or method. which ever it is same scenario.
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]];
dispatch_async(dispatch_get_main_queue(), ^{
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *res, NSData *dat, NSError *err)
{
// procress data
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// back ground work after network data received
dispatch_async(dispatch_get_main_queue(), ^{
// finally update UI
});
});
}];
});
});
You can us GCD, like that:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// put here your background code. This will run in background thread.
dispatch_async(dispatch_get_main_queue(), ^{
// this will run in main thread. update your UI here.
});
})
But you need to understand how blocks work for example. Try it.
Use GCD
create your dispatch object
dispatch_queue_t yourDispatch;
yourDispatch=dispatch_queue_create("makeSlideFast",nil);
then use it
dispatch_async(yourDispatch,^{
//your code here
//use this to make uichanges on main thread
dispatch_async(dispatch_get_main_queue(), ^(void) {
});
});
Do ui on main thread only by using
dispatch_async(dispatch_get_main_queue(), ^(void) {
});
Then release it
dispatch_release(yourDispatch);
Here is tutorial
There are a lot of ways:
1 . Grand Central Dispatch
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//Your code
});
2 . NSThread
[NSThread detachNewThreadSelector:#selector(yourMethod:) toTarget:self withObject:parameterArray];
3 . NSObject - performSelectorInBackground
[self performSelectorInBackground:#selector(yourMethod:) withObject:parameterArray];

update ui while running in a loop

I have a read-only text field that I use as a log display. I have a operation that removes all the files in app's document directory. I want to insert a line of log when I remove each file. But the text field is only updated when the whole operation got finished. How do I fix this?
Here is my code:
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
for (NSString *fileName in array) {
NSString *filePath = [DOCUMENT_PATH_VALUE stringByAppendingFormat:#"/%#", fileName];
[fm removeItemAtPath:filePath error:&error];
if (!error) {
NSString *log = [NSString stringWithFormat:#"removed success: %#", fileName];
[self logThis:log];
}else{
NSString *log = [NSString stringWithFormat:#"remove failed: %#, %#", fileName, [error localizedDescription] ];
[self logThis:log];
-(void)logThis:(NSString*) text{
NSRange range = NSMakeRange([updateLogTextView.text length], [text length]);
updateLogTextView.text = [updateLogTextView.text stringByAppendingFormat:#"%#\n", text];
[updateLogTextView scrollRangeToVisible:range];
}
You need to move your long-running operation into a background thread/queue and make sure that your UI-updating code is always executed on the main thread.
Example:
- (void)processFiles:(NSArray *)array
{
//You need to create your own autorelease pool in background threads:
NSAutoreleasePool *pool = [NSAutoreleasePool new];
//...
NSString *log = ...;
[self performSelectorOnMainThread:#selector(logThis:) withObject:log waitUntilDone:NO];
//...
[pool release];
}
You would then start your operation using:
[self performSelectorInBackground:#selector(processFiles:) withObject:files];
Using Grand Central Dispatch with blocks would be another option.
Refer to the Threading Programming Guide and the Concurrency Programming Guide for more in-depth information.
You need to look into Threading and making that call Asynchronous. You are currently blocking your main thread (assuming it's called from there). You need to separate the two so a separate thread does the heavy operation while your main thread is updated with the new text in the UI.
I've just started ios dev but sounds like you need to redraw that UIText I'm not sure how though I'd assume that when your log writes that you could dealloc your original UIText object the realloc it and it should redraw with the new log message. Only problem is that it will most likely update very very fast so redrawing wouldnt be worth it unless ios has a sleep function?
Something like:
Alloc and init text
Then in for loop after each write or new message in log dealloc your text and realloc and init your text
This should redraw the UIText object and update the text, if it doesnt expect a memory write error/app crash

blocks in uitableview didSelectRowAtIndexPath and passing autoreleased vars around causing nil behavior

I'm attempting to download an MP3 file from my server when a user selects a row using blocks and a dispatch_queue. Things seem to work great about 80% of the time.
Here is my thought process:
When the user selects the row, update the UI to show that a download has begun (spinner)
Add a block to a background thread to begin the download in the background (I use the thread blocking dataWithContentsOfURL method. I believe this is ok because it is on an async dispatch queue.
on the main_queue, When the download is completed, create an AVAudioPlayer with the data from the dispatch queue.
on the main_queue, Play sound file.
on the main_queue, update UI to reflect that the sound is playing and the download is complete.
Like I said, this works perfectly 80% of the time. When something works 'most' of the time, it screams memory management issues.
So here is the snippet:
//download url
dispatch_queue_t downloadQueue = dispatch_queue_create("com.mycompany.audiodownload", NULL);
dispatch_retain(downloadQueue);
JAAudioMessageEvent *event = [self.cheers objectAtIndex:indexPath.row];
dispatch_async(downloadQueue, ^{
NSURL *sampleURL = [NSURL URLWithString:event.sample];
NSLog(#"start downloading file %#", sampleURL);
NSData *data = [NSData dataWithContentsOfURL:sampleURL];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"play file %#", (data == nil) ? #"data file is nil":#"");
NSError *error;
self.eventPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
if (error) {
NSLog(#"there was an error when playing %#", error);
}
[self.eventPlayer play];
self.eventPlayer.delegate = self;
[activityIndicator removeFromSuperview];
[activityIndicator release];
self.eventInProgress = NO;
});
dispatch_release(downloadQueue);
});
So the issue is that during this 20% of the time when things go wrong, it would seem as though the event is being released and I'm given a null url. I fetch a null url and I get an error when attempting to play said null url.
So the question becomes, how do I manage the event object so that it persists over the 2 async threads?
Thanks in advance

Three20 TTURLRequest Synchronous Cache (make it async or thread it)?

I'm currently working with the Three20 library to implement some caching for an iPhone project. It has been a huge help for speeding up my table views and page loads, however I'm running into a small issue:
I have subclassed TTURLImageResponse to resize/crop images that are retrieved remotely before caching them so that I don't have to resize them each time I get them back from the server/cache. This works very well and the images load quite quickly, however when images are returned from the cache in the Three20 library they are returned synchronously. This results in a noticeable delay when firing off 10-20 image requests that come back from the cache. Since they are coming back synchronously, they are blocking my UI thread and the user is left waiting for a couple seconds before they see the images that came back from the cache.
I have attempted to thread the operation by doing the following:
- (void) setupImages
{
if(imageList != nil &&
[imageList count] > 0)
{
[NSThread detachNewThreadSelector:#selector(fillImages) toTarget:self withObject:nil];
}
else
{
lblMessage.hidden = NO;
lblMessage.text = #"There are no images. Try refreshing the current list.";
lblTitle.text = #"";
lblAuthor.text = #"";
}
}
- (void)fillImages
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i=0;
TTURLRequest *request;
for (MyImage *imageObj in imageList)
{
NSString *imageUrl = [[[imageObj thumbnail_lg] image_url] absoluteString];
if(imageUrl != nil)
{
request = [TTURLRequest requestWithURL:imageUrl delegate:self];
MyTTURLImageResponse *responseObj = [[[MyTTURLImageResponse alloc] init] autorelease];
responseObj.index = i;
request.cachePolicy = TTURLRequestCachePolicyDisk;
request.response = responseObj;
request.httpMethod = #"GET";
[request send];
i++;
}
}
[pool release];
}
//When the fillImages selector is spawned on its own thread it does not callback to this method, therefore images are never loaded!!
- (void)requestDidFinishLoad:(TTURLRequest*)request
{
MyTTURLImageResponse *response = request.response;
UIImage *loadedImage = response.image;
int imageIndex = response.index;
//this now happens as the image is cached :)
//loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(200, 200)];
[myImageDisplay setImage:loadedImage forIndex:imageIndex];
}
However the threading doesn't appear to be working (the callback is on the main thread...). Does anyone know of a way to retrieve from the Three20 Cache either ASYNCHRONOUSLY or a way of threading the Three20 TTURLRequest so that it's at least doing this in the background?

NSAutoreleasePool issues on the iPhone

I have a small iPhone app which has a button on the first view. When I select this button I load up my new view which has an image on it. I'm currently using the following code to load the image from an online source on a separate thread, whilst allowing the user to continue controlling the application:
- (void) loadImageTest
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [[NSURL alloc] init];
url = [NSURL URLWithString:logoPath];
NSData *data = [[NSData alloc] init];
data = [NSData dataWithContentsOfURL:url];
loadingImage = [UIImage imageWithData:data];
titleLogoImage.image = loadingImage;
//[pool drain];
[pool release];
}
This is called from this line of code in the new view's init:
[NSThread detachNewThreadSelector:#selector(loadImageTest) toTarget:self withObject:nil];
Now this works fine (ish), but if I close the new view and then load a new one up again in quick succession (or just after-wards in some cases) it will bomb out with the classic EXC_BAD_ACCESS. I'm sure that this is due to the code within the thread pool, but can anyone see why this is happening?
Thanks
Yours:
// This is ok, you might try using NSURLConnections asynchronous methods instead of making
// your own thread.
[NSThread detachNewThreadSelector:#selector(loadImageTest) toTarget:self withObject:nil];
- (void)loadImageTest
{
// This is fine
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// you're making and then abandoning this url object so it will leak
NSURL *url = [[NSURL alloc] init];
// this is fine
url = [NSURL URLWithString:logoPath];
// again making and abandoning an object
NSData *data = [[NSData alloc] init];
data = [NSData dataWithContentsOfURL:url];
// this works, but is not thread safe,
// can't operate on UIKit objects off the
// main thread
loadingImage = [UIImage imageWithData:data];
titleLogoImage.image = loadingImage;
// this is fine
//[pool drain];
[pool release];
}
Cleaned up to make things thread safe, etc. Should help:
// I'm assuming you have a iVar for logoPath but we don't want to
// make threaded calls to an iVar (it's not mutable, so you could do it, but it's just bad form)
// If i'm wrong about logoPath being an iVar don't sweat copying it.
- (void)whateverMethodYouWant
{
NSString *aLogoPath = [[logoPath copy] autorelease];
[NSThread detachNewThreadSelector:#selector(loadImageForPath:) toTarget:self withObject:aLogoPath];
}
- (void)loadImageForPath:(NSString *)aLogoPath
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:aLogoPath]];
// the performSelector will retain the data until the method can be performed
[self performSelectorOnMainThread:#selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO];
[pool release];
}
- (void)setImageForTitleLogo:(NSData *)imgData
{
// This part is not strictly necessary
// but it's a nice way to wrap a method that needs to happen on the main thread.
if ([NSThread isMainThread])
{
// make the image (i'm assuming you meant loadingImage to be a local scope variable)
UIImage *loadingImage = [UIImage imageWithData:imgData];
// make sure the button still exists
// also make sure you're setting any references to this button to nil when you're releasing and making new views
// so that you're not addressing a button that's been released
// (assigning the image to the button will cause the button to retain it for you)
if (titleLogoImage != nil)
titleLogoImage.image = loadingImage;
}
else
{
[self performSelectorOnMainThread:#selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO];
}
}
You're doing weird stuff.
NSURL *url = [[NSURL alloc] init];
means you create an NSURL object, which you own.
url = [NSURL URLWithString:logoPath];
This means you create another NSURL object, which is autoreleased. The NSURL you just created, just leaks. The same goes for the NSData here.
The loadingImage must be retained by titleLogoImage, or it will be deallocated on the drain of your NSAutoReleasePool. What is titleLogoImage and does it retain image?
edit ps: what also disturbes me, is that loadingImage is not confined to the scope of the function. To cut things short:
NSURL *url = [NSURL URLWithString:logoPath];
NSData *data = [NSData dataWithContentsOfURL:url];
titleLogoImage.image = [UIImage imageWithData:data];
may save some headaches, at least.
There is nothing in the posted code that would trigger a crash. Depending on how titleLogoImage is defined, it might be affects by the autorelease.
However, beyond the problems outlined by mvds, you have no pressing needs for a localized autorelease pool. It will do nothing here but cause you trouble.
Autorelease pools are dangerous and not for beginners. They will kill any autoreleased objects in them. You generally only create your own pool when you are rapidly creating a large number of memory intensive objects. That does not appear to be the case here.
Despite the attention given them, custom pools are seldom used. After over a decade Apple API work, I can probably count on my fingers the number of times I have used my own custom pool.