Create a new delegate class for each asynchronous image download?
Posted
by Charles S.
on Stack Overflow
See other posts from Stack Overflow
or by Charles S.
Published on 2010-03-26T04:46:42Z
Indexed on
2010/03/26
4:53 UTC
Read the original article
Hit count: 297
First, I'm using an NSURLConnection to download JSON data from twitter. Then, I'm using a second NSURLConnection to download corresponding user avatar images (the urls to the images are parsed from the first data download).
For the first data connection, I have my TwitterViewController set as the NSURLConnection delegate. I've created a separate class (ImageDownloadDelegate) to function as the delegate for a second NSURLConnection that handles the images. After the tweets are finished downloading, I'm using this code to get the avatars:
for(int j=0; j<[self.tweets count]; j++){
ImageDownloadDelegate *imgDelegate = [[ImageDownloadDelegate alloc] init];
Tweet *myTweet = [self.tweets objectAtIndex:j];
imgDelegate.tweet = myTweet;
imgDelegate.table = timeline;
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:myTweet.imageURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
imgConnection = [[NSURLConnection alloc] initWithRequest:request delegate:imgDelegate];
[imgDelegate release];
}
So basically a new instance of the delegate class is created for each image that needs to be downloaded. Is this the best way to go about this? But then there's no way to figure out which image is associate with which tweet, correct?
The algorithm works fine... I'm just wondering if I'm going about it the most efficient way.
© Stack Overflow or respective owner