0

I have written following code to reload a UITableView from a NSInvocationOperation. However the interface does not update for a long time after calling [tableview reloadData].

Apple documentation says that delegate method won't be called within NSOperation.

NSOperationQueue *queue = [NSOperationQueue new];

NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                            initWithTarget:self
                                            selector:@selector(connectToServer)
                                            object:nil];

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

- (void) connectToServer
{
    ...
    ...
    [tableview reloadData];
}
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
nameless
  • 809
  • 3
  • 9
  • 27

1 Answers1

2

The problem is that UI updates must occur on the main thread, and reloadData is being called from a background thread via the NSOperationQueue.

You can use the NSObject method performSelectorOnMainThread:withObject:waitUntilDone: to ensure such updates occur on the main thread.

- (void) connectToServer
{
    ...
    ...
    [tableView performSelectorOnMainThread:@selector(reloadData)
            withObject:nil
            waitUntilDone:NO];
}

Additionally, the NSOperationQueue should not be a local variable. It should be a retained property of this class, and only released in dealloc.

Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77