views:

147

answers:

2

The Situation:
Somewhere in my app I start downloading data from my server. Before downloading starts, I would like to update a UILabel to say @"Now Downloading...". And set it back to blank when downloading is over.


The Problem:
It seems like the download takes up all of the computers attention, and the UILabel never gets updated until the very end (at which downloading is already over) and so is set back to blank (or, never-visible in real time).


Question:
How can I SIMPLY update my UILabel to say "Now Downloading" just before the download?

A: 

If you use NSURLRequest -> NSURLConnection and the NSURLConnection's delegate methods this will perform the download in the background and will notify the delegate of incoming data. This will also allow you to display a progress.

Felix
Naaa. Thanks anyway but I'm not going down that road again. I tried doing it that way a while back but ran into a world of pain. So, I'm just gonna stick to my method of [NSString stringFromContentsOfURL:URL] or whatever for now lol. Should I be looking at threads?
RexOnRoids
No, you should be looking at NSURLRequest and NSURLConnection. That is what they are designed for. That you ran into a world of pain is an entirely different issue; pose a question that contains your pain.
bbum
I would guess that using the NSURLConnection plus delegate is easier than doing all the threading yourself. The sample codes provided by Apple should be clear enough.
Felix
Also: keep in mind that UIKit is not Thread-safe. Hence all UI manipulations should be carried out on the mainthread.
Felix
Yeah but I hate reading apples boring stuff lol. Oh, well. I'm just hard-headed.
RexOnRoids
+3  A: 
label.text = @"Downloading";
NSOperationQueue *operationQueue = [[NSOperationQueue]alloc]init];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:@"http://www.google.com"];
[operationQueue addOperation:operation];
[operation release];


- (void)download:(NSString *)url
{
    // do the download
    [self performSelectorOnMainThread:@selector(didFinishDownload) withObject:nil waitUntilDone:NO];
}

- (void)didFinishDownload
{
   label.text = @"";
}
marcc
You have an extra "ance" there :). Also, will he need an autorelease pool?
Frank Schmitt
Hey, thanks. But -- How do I pass in arguments for @selector(download) ?? It takes a single NSString
RexOnRoids
This will leak, unless there's some way to `release` the `operationQueue` after `-didFinishDownload` is called or all download operations are complete.
Alex Reynolds
Updated to show passing arguments into download. Also, yes, this leaks. Make the operationQueue a member variable.
marcc