Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,558
6,058
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:

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..."

Edit:
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.