Solved

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

Posted on 2010-08-26
7
1,381 Views
Last Modified: 2013-11-25
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



0
Comment
Question by:mattasks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33538756
You can use threads in Objective-C too. You will see the background job in the following tutorial:
Threading tutorial using NSthread in iPhone SDK (Objective-C)
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

Or this article:
Programming threaded processes in iPhone      
http://www.eigo.co.uk/Programming-threaded-processes-in-iPhone.aspx

In iOS Reference Library you can find "Threading Programming Guide": http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/Multithreading/CreatingThreads/CreatingThreads.html
0
 

Author Comment

by:mattasks
ID: 33538940
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.
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 500 total points
ID: 33539015
From my point of view it's all the same as in C#.
There is a possibility to use POSIX threads as in C++ too - there is a paragraph about it in
http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/Multithreading/CreatingThreads/CreatingThreads.html

But in your case the Objective-C NSThread is fine.
>>I don't have a firm understanding of whether things are asynchronous within objective-C.
Same you can say about Win32 API or about any other framework.
If it is not mentioned that a function works asynchronously we use it as a synchronous function. If we need something happening asynchronously we wrap it in a thread and we need to worry about the thread synchronization.

The code you posted about dataWithContentOfURL can be wrapped in the thread. If you wish.
Somehow I remember another code with a complete flag or method that will be called when an URL operation will be finished.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mattasks
ID: 33539291
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?



0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33539340
>>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.
0
 

Author Closing Comment

by:mattasks
ID: 33547350
Great, thanks for your help.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33548268
You are welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question