views:

44

answers:

1

I've got a thread (specifically an NSOperation) that runs to load some images for me for a scroll view when my main view asks for them. Any number of these NSOperations can be queued at once. So it goes with the filepath I give it and loads the image from the disk (as UIImages) and then sends the object back to my mainview by using performSelectorOnMainThread: and passing my mainview an NSDictionary of the object, and an image ID value. My main view is then supposed to insert the image object and the image ID string into an NSMutableDictionary that it has for the mainview to be able to use. I've verified that the NSMutableDictionary is allocated and initialized fine, but when the method the NSOperation calls tries to add the objects to the dictionary nothing happens. I've verified that the object and string i get from the dictionary the thread sent me are not null or anything but yet it doesn't work. Am I not doing something right or using a bad technique? What would anyone suggest to do in a situation like this where I need to add UIImages to an NSMutableDictionary from a thread? Thanks so much!

Here's the NSOperation code I use:

- (void)main {
    NSString *filePath = [applicaitonAPI getFilePathForCachedImageWithID:imageID andSize:imageSize];
    UIImage *returnImage = [[UIImage alloc] initWithContentsOfFile:filePath];

    if (returnImage) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
        [dict setObject:returnImage forKey:@"IMAGE"];
        [dict setValue:[NSString stringWithFormat:@"%d", imageID] forKey:@"IMAGE_ID"];
        NSDictionary *returnDict = [[NSDictionary alloc] initWithDictionary:dict];
        [dict release];
        [mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:returnDict waitUntilDone:NO];
        [returnDict release];
    }
}

And here's the method on the main thread:

- (void)imageLoaderLoadedImage:(NSDictionary *)dict {
  UIImage *loadedImage = [dict objectForKey:@"IMAGE"];
  NSString *loadedImage = [dict valueForKey:@"IMAGE_ID"];
  [imagesInMemoryDictionary setObject:loadedImage forKey:loadedImageID];
  [self drawItemsToScrollView];
}
+1  A: 
[mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:nil waitUntilDone:NO];

You're not passing returnDict as the parameter to the method. You're passing nil.

A couple of other thoughts:

  • you don't need to create returnDict. You can just use dict as the method parameter.
  • you're leaking returnImage.

edit

Since you apparently are passing returnDict as the parameter to the method, my other guess would be that mainViewController is nil. Other than that, your code looks functional.

Dave DeLong
Exactly, when you call a method using a selector you should pass the args in "withObject".Thanks to @Dave DeLong in the first place.
sicario
Oops thats actually a change I made in the code while testing something, I really am sending the dict. Sorry.
Alexander
Well it's weird, because I know that mainviewcontroller the method is getting called and even the objects I've checked are not nil and the string that is passed is logged and comes out fine. But when I try to insert it into the dictionary nothing happens! So frustrating!
Alexander
And the weird thing is I tried doing a simple test as well with just adding a button on the mainview that would when pressed add a test value to that same dictionary and *that* did work. This is really weird.
Alexander