Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread Display Modes
Old Nov 22, 2009, 10:11 PM   #1
macrumors 603
ArtOfWarfare's Avatar
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
NSOperation Help...

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.
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)

Last edited by ArtOfWarfare; Nov 22, 2009 at 11:03 PM.
ArtOfWarfare is offline   0 Reply With Quote
Old Nov 23, 2009, 04:03 PM   #2
Moderator emeritus
kainjow's Avatar
Join Date: Jun 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.
kainjow is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Thread safety with NSOperation Qaanol Mac Programming 5 Aug 25, 2013 04:36 PM

Forum Jump

All times are GMT -5. The time now is 02:58 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC