NSOperation Help...

Discussion in 'iOS Programming' started by ArtOfWarfare, Nov 22, 2009.

  1. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I need help with two things... first off, I'd like to pass i, just a C integer, as an object in an NSInvocationOperation.

    Second thing... well, am I using NSOperations properly? The idea is that by using it, the interface won't become unresponsive while it's loading the data.

    Here's my code:

    		infoQueue = [[NSOperationQueue alloc] init];
    		infoQueue.maxConcurrentOperationCount = 1;
    		int i = 0;
    		for (i = 0; i < [users count]; i++)
    			NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector: @selector(loadInfo) object:i];
    			[infoQueue addOperation: operation];
    			[operation release];
        return self;
    - (void)loadInfo:i
    	[userInfoArray addObject: [TwitterHelper fetchInfoForUsername: [users objectAtIndex: i]]];
    	[self reloadRowsAtIndexPaths:[NSArray arrayWithObject: i] withRowAnimation: UITableViewRowAnimationNone];
    I have set it up so that if the image hasn't been downloaded yet, it has a label "Loading..."

    Never mind, I got it to work. It seems the issue was with my signature, all I did was change
    "selector: @selector(loadInfo)" to
    "selector: @selector(loadInfo: )"

    Mods, feel free to delete this thread... although I'm leaving it up for incase someone else ever has the same issue.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    You got it to compile, or you got it to actually run and work as expected? Because if the former then that code will most likely fail to work at all. First off, "i" as an int is not an object. You can't pass it to methods that take an id, which is an object. Wrap it up in an NSNumber. Second, your loadInfo: method should have a specific data type, which should be the NSNumber. Then you will need to get the intValue out of it when using it as an array index. Third, NSOperation will operate on a background thread, and you're calling UIKit methods from that method, which should only happen on the main thread.

    If you don't know how to pass primitives as objects, I highly recommend you don't try to add threading to your application. You may want to master the C/Obj-C basics first.

Share This Page