Mastering the art of threading using NSOperation is the first part of this assignment. I unfortunately got a bit stuck in the lecture examples. I was trying to implement the example code that I had seen without a solid understanding of what it was doing. This led to a few nights of frustration. I finally decided to scrap my currant process, go back to the code and try to really understand what it was doing. (I know, this is something one should always do, but in my zest to have a working prototype I lost sight of this).
My biggest problem here was that I needed to understand the order of operations a little better. The first thing I need my app to do is get the list of usernames. So, in my table view controllers initWithStyle method I read the names from the TwitterUsers.plist and initialize my names NSArray with the results.
usersBundle = [[NSBundle mainBundle] pathForResource:@”TwitterUsers” ofType:@”plist”];
names = [[NSArray alloc] initWithContentsOfFile:usersBundle];
I’ll also use this method to init my NSOperationQueue. I’ll be needing this very soon.
In my viewWillAppear method I’ll call the method to get the list of details for each person. This is going to include the URLs for their respective images. Except, I don’t really call the method that does this. I call the method that adds the method that does this to an NSOperationQueue (getListOfPeople).
{
NSLog(@”(void)getListOfPeople”);
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(syncLoadingPeopleList) object:nil];
[operationQueue addOperation:operation];
[operation release];
}
So, syncLoadingPeopleList is the method that loads the people data into the people array. When this is finished I call the didFinishLoadingPeopleList back in the main thread.
The didFinishLoadingPeopleList iterates through the people and adds the image URL to my imageURLList NSMutableArray. Upon writing this, it occurs to me that I could probably just as well populate my imageURLList array in the syncLoadingPeopleList method at the same time I am populating the people array…
Here’s something that gave me some trouble initially. Chalk it up to a dumb mistake. At this point I still had my numberOfRowsInSection method looking at the size of my names array (perfectly ok in Presence 2). The problem here was that I was looking for information in my ImageURLList, which was populated by my people array, which at that point may not have had a single person loaded yet. I was getting all sorts of invalid index errors. Once I switched that out with the people array I was good to go.
So now I’ve got my image URLs, but still no images. Images need to be downloaded from the net and loaded into the table cell. Obviously this calls for some more threading. In my cellForRowAtIndexPath method where I would normally set my image to equal an already existing image, I’m setting it to equal the result of a cached image array lookup. Here’s how this works:
- cellForRowAtIndexPath is called nd wants an image
- image is set to the cached image
cell.image = [self cachedImageForURL:[imageURLList objectAtIndex:indexPath.row]];
- the cachedImageForURL method takes a NSURL as a param and looks to see if it’s NSMutableArray counterpart (cachedImages) already has this image
id cachedObject = [cachedImages objectForKey:url];
if(cachedObject == nil){ - If so, then it checks to make sure it is an NSURL, and sets it to nil if not (remember it’s ok to send a nil message)
else if(![cachedObject isKindOfClass:[UIImage class]]){
cachedObject = nil;
} - If not, then it starts the process of downloading the image
ImageLoadingOperation *operation = [[ImageLoadingOperation alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImage:)];
[operationQueue addOperation:operation];
[operation release];
- the cachedImageForURL method takes a NSURL as a param and looks to see if it’s NSMutableArray counterpart (cachedImages) already has this image
- This executes as many times as there are people in the list, and calls didFinishLoadingImage as soon as the image is loaded and ready for use. This adds the image to the cashedImages array and reloads the table data.
-(void)didFinishLoadingImage:(NSDictionary *)result
{
NSURL *url = [result objectForKey:@"url"];
UIImage *image = [result objectForKey:@"image"];[cachedImages setObject:image forKey:url];
[self.tableView reloadData];
}
…and we have our image
Looks great man, keep it coming! I hope to get back to the assignments this week, maybe during the Wednesday meet at Bard Coffee. Inspiring stuff, thanks for posting
По моему мнению Вы не правы. Давайте обсудим это….
бухгалтер, бухгалтер-оператор, менеджер, администратор This led to a few nights of frustration. I […….
Buy:Accutane.Lumigan.Mega Hoodia.Zyban.Synthroid.Prevacid.100% Pure Okinawan Coral Calcium.Human Growth Hormone.Valtrex.Prednisolone.Zovirax.Petcam (Metacam) Oral Suspension.Actos.Nexium.Retin-A.Arimidex….
Buy:Viagra.Soma.Tramadol.VPXL.Viagra Super Force.Viagra Professional.Zithromax.Cialis.Levitra.Viagra Soft Tabs.Viagra Super Active+.Super Active ED Pack.Maxaman.Cialis Super Active+.Cialis Professional.Cialis Soft Tabs.Propecia….
Espresso http://lgamingfvh.04FORDPARTS.US/tag/carezza+gaggia+Espresso/ : carezza…
Espresso…