I am running into an strange behavior when using NSOpeation.
I am calling a function (-createTagView) that creates an UIButton to then add it to a view.
For some reason it’s not adding them. If I call the function from outside the NSOperation everything works fine.
Any ideas? Thanks.
This how I create the NSOperation (within a ViewController object)
> NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createTagView:) object:data];
> [operationQueue addOperation:operation];
> [operation release];
And this is the function called ([Tag view] is the UIButton):
-(void) createTagView:(NSMutableArray *) data
{
NSInteger t_id = (NSInteger)[data objectAtIndex:0];
NSString *t_name = (NSString *)[data objectAtIndex:1];
NSString *t_rawname = (NSString *)[data objectAtIndex:2];
Tag *t = [[Tag alloc] initWithId:(NSInteger)t_id name:t_name rawname:t_rawname];
[self.view addSubview:[t view]];
[t release];
}
NSOperation uses a separate thread to run, you must call [addSubview: ] within the main thread. There is a method [object performSelectorOnMainThread:withObject:waitUntilDone:] – you can use it to add the subview.