Link to home
Start Free TrialLog in
Avatar of mattasks
mattasks

asked on

Timing in objective-c for iphone - How do I wait until data returned from URL without freezing the User Interface?

in an iPhone app I have a method in which I request some data from a URL, and then display this data.

If I loop until the data != nil, then this freezes my UI until the URLs have returned, right?
Do I need to do some multithreading to free the UI up while my URLs load?
In C# I would use a simple BackgroundWorker.  Should I be using something similar in objective-C?
How can I do this better?

	int i = 0;
	while (i != 1) {
		if (myData1 != nil && myData2 != nil && myData3 != nil && myData4 != nil && myData5 != nil && myData6 != nil) {
			//can you add a nil object to an array
			[myMA addObject:myData1];
			[myMA addObject:myData2];
			[myMA addObject:myData3];
			[myMA addObject:myData4];
			[myMA addObject:myData5];
			[myMA addObject:myData6];
			i = 1;
		}
	}

Open in new window



ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mattasks
mattasks

ASKER

Do I even need multiple threads?
I don't have a firm understanding of whether things are asynchronous within objective-C.
What would happen if I didn't use a loop to wait for the URLs to return, and just used this code:

NSURL *myURL = [NSURL URLWithString:@"http://www.apple.com/image.jpg"]
NSData *myData1 = [NSData dataWithContentsOfURL: myURL1];
[myUIImageView setImage:[UIImage imageWithData:myData1]];


If the URL takes a few seconds to return, what will happen on my User Interface?  I don't know how to test this, as my URLs are returning very quickly.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, so I should trust the apple docs to tell me if a function is asynchronous or not?

Eg. Do my three lines of code would work synchronously, with [myUIImageView setImage] only executing once the previous lines finished executing?  Therefore the UI freezes?

NSURL *myURL = [NSURL URLWithString:@"http://www.apple.com/image.jpg"]
NSData *myData1 = [NSData dataWithContentsOfURL: myURL1];
[myUIImageView setImage:[UIImage imageWithData:myData1]];


Whereas if I was using a UIWebView and calling loadRequest (the docs say it's asynchronous), then my UI would not freeze?



>>then my UI would not freeze?
Why not? It's iPhone, it's not a Mac Pro. If a thread takes 100% CPU or another physical resource, the device stuck.
In your application if you have a long task you wrap it in a thread and that allow the UI of your application to response fast. But if your background thread will take 100% CPU the device is stuck anyway.
If your application does not respond to the user, it's bad. Everybody uses this loadRequest and everybody's happy.
Great, thanks for your help.
You are welcome.