iphoneuiimagensoperationqueuensinvocationoperation

Can't seem to call NSInvocationOperation with an NSArray


I am trying to load an image in the background from a url. The code works great if all I pass is the NSUrl. If I try to pass an NSArray with additional variables, it never gets called:

This code works great, LoadImage2 is called which in turn nicely calls ImageLoaded2.

- (void)LoadBackgroundImage2: (char*)pImageURL
{

    NSString* pImageURLString = [NSString stringWithFormat:@"%s", pImageURL];

    NSLog( @"LoadBackgroundImage2: %@", pImageURLString );

    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(LoadImage2:)
                                    object:pImageURLString];
    [queue addOperation:operation];
    [operation release];
}


- (void)LoadImage2: (NSString*)pImageURL
{
    NSLog( @"LoadImage2: %@", pImageURL );

    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pImageURL]];
    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
    [imageData release];
    [self performSelectorOnMainThread:@selector(ImageLoaded2:) withObject:image waitUntilDone:NO];
}

This code does not work. LoadImage never gets called:

- (void)LoadBackgroundImage: (char*)pImageURL :(int)textureID :(int)textureType
{

    printf( "LoadBackgroundImage( %s, %d, %d)\n", pImageURL, textureID, textureType );

    NSString* pImageURLString = [NSString stringWithFormat:@"%s", pImageURL];

    NSArray* pUrlAndReferences = [[[NSArray alloc] initWithObjects: pImageURLString, textureID, textureType, nil] autorelease];

    NSOperationQueue *queue = [[NSOperationQueue new] autorelease];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(LoadImage:)
                                    object:pUrlAndReferences];

    [queue addOperation:operation];
    [operation release];
}


- (void)LoadImage: (NSArray*)pUrlAndReferences
{
    NSString* pImageUrl = [pUrlAndReferences objectAtIndex: 0];
    int textureId = [ [ pUrlAndReferences objectAtIndex: 1 ] intValue ];
    int textureType = [ [ pUrlAndReferences objectAtIndex: 2 ] intValue ];

    NSLog( @"\n\nLoadImage: %@, %d, %d\n", pImageUrl, textureId, textureType );

    NSData* pImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pImageUrl]];
    UIImage* pImage = [[[UIImage alloc] initWithData:pImageData] autorelease];

    NSArray* pImageAndReferences = [[[NSArray alloc] initWithObjects: pImage, textureId, textureType, nil] autorelease];

    [pImageData release];
    [self performSelectorOnMainThread:@selector(ImageLoaded:) withObject:pImageAndReferences waitUntilDone:NO];
}

Anyone have any ideas why LoadImage doesn't get called?

Thanks.


Solution

  • My guess is your not retaining your queue. Here's what's happening

    1. Your array (autoreleased) goes into the NSInvocationOperation and it's being retained (no problem)
    2. Your NSInvocationOperation goes into the queue (retained) and then it's released. No problem here since the retain count is still one: 1 (alloc) + 1 (retain) - 1 (release) = 1 = not dealloced.
    3. Your queue is alloced (new = alloc+init) and then autoreleased, but it's not being retained elsewhere. Here comes the problem: since you have autoreleased the queue, once the method LoadBackgroundImage is finished the queue has a retain count of 0 and it's automatically released, therefore, your Invocation won't be executed.

    You can try if that's the problem by just removing the autorelease call from the queue. If I'm correct your code should work. But be aware that that is not a good solution because you're loosing memory. It's just to see if it works.

    You should definitely make a class, a singleton, an instance variable or whatever you like to retain that instance of the queue. Also, it's a good idea to only have one queue for all the LoadBackgroundImage calls instead of creating a new queue every time.