Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I have a screen with grid 3x3 and I have to download 9 images from 9 different Image URLs.

Image downloading is Asynchronous.

For each image I have to show activity indicator. As long as download completes activity indicator should be stopped.

How can I achieve this?

See attached image for clear understanding.
 

Attachments

  • Progress.png
    Progress.png
    10.8 KB · Views: 324
Last edited:
What have you tried? What were the results? What exactly are you stuck on?

Solved.
Please check my approach whether I am correct on not.

1. I have created a class AsyncImage which is responsible for getting image from the server and a delegate AsyncImageDelegate.

2. I have a ViewController class with Grid Layout which has 9 images (see my screen in 1st post). Every image is a type of AsyncImage. This ViewController is implementing AsyncImageDelegate which has downloadFinished: function.

3. AsyncImageDelegate is the delegate for all 9 images.

4. On viewDidLoad I call loadImage: method for each images. Then it starts image downloading asynchronously and also starts 9 Activity Indicator.

5. As soon as one by one images downloads it called downloadFinished: method, in this method I stop ActivityIndicator based on images ( I m setting tag for each images from 1 to 9).

If steps are not clear we can have more threads for this discussion.
 
If it does what you want it to, then it's correct, is it not? Without seeing your code, I can't say whether you're leaking or doing anything inefficiently or not.
 
If it does what you want it to, then it's correct, is it not? Without seeing your code, I can't say whether you're leaking or doing anything inefficiently or not.

make sense, right now app is working fine for me, later on I'll also send you code snippet.

Thanks
 
Solved.
Please check my approach whether I am correct on not.

1. I have created a class AsyncImage which is responsible for getting image from the server and a delegate AsyncImageDelegate.

2. I have a ViewController class with Grid Layout which has 9 images (see my screen in 1st post). Every image is a type of AsyncImage. This ViewController is implementing AsyncImageDelegate which has downloadFinished: function.

3. AsyncImageDelegate is the delegate for all 9 images.

4. On viewDidLoad I call loadImage: method for each images. Then it starts image downloading asynchronously and also starts 9 Activity Indicator.

5. As soon as one by one images downloads it called downloadFinished: method, in this method I stop ActivityIndicator based on images ( I m setting tag for each images from 1 to 9).

If steps are not clear we can have more threads for this discussion.


Your approach sounds good, but as the other poster said, it's hard to tell without seeing the code. Did you write your own async download code? That's really the tricky bit.
 
Don't use threads for async downloads!

Shouldn't be that hard, create an custom class with delegate patterns for downloading on a async image call, or just call the urlconnection a different thread?

Calling NSURLConnection from a different thread is not the way to go. Threads are expensive for the system to set up and run. They tie up physical memory for the lifetime of your app, and make it more likely that the system will kill you to free up memory when another app is frontmost.

It's worth going to Apple's dev forum and reading posts by an Apple engineer who calls himself Quinn the Eskimo. He has a very long post about the evils of threads that is well worth reading.

NSURLConnection has full support for async operation built in. That async support uses shared system resources that are fine-tuned for efficiency. Use NSURLConnection and let Apple do the heavy lifting for you.

Under iOS 5 there are even new block-based NSURLConnection methods that make it easier to handle an async download without having to write a bunch of delegate methods.
 
Calling NSURLConnection from a different thread is not the way to go. Threads are expensive for the system to set up and run. They tie up physical memory for the lifetime of your app, and make it more likely that the system will kill you to free up memory when another app is frontmost.

It's worth going to Apple's dev forum and reading posts by an Apple engineer who calls himself Quinn the Eskimo. He has a very long post about the evils of threads that is well worth reading.

NSURLConnection has full support for async operation built in. That async support uses shared system resources that are fine-tuned for efficiency. Use NSURLConnection and let Apple do the heavy lifting for you.

Under iOS 5 there are even new block-based NSURLConnection methods that make it easier to handle an async download without having to write a bunch of delegate methods.

You have written it down perfectly. In our apps we use the async methods for support pre iOS5 and for iOS5 We use the block methods which are really handy.
I remember that you always took time to write these long, well written posts on iPhoneDevSDK, welcome here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.