I wonder how run sqlite querys in the background as suggested in http://stackoverflow.com/questions/155964/what-are-best-practices-that-you-use-when-writing-objective-c-and-cocoa
This is my try:
- (void) run:(NSString *)sql {
NSArray *data = [NSArray arrayWithObjects:
sql,
[self returnItemClass],
nil];
NSInvocationOperation *operation =
[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(loadRecords:)
object:data];
[self.queue addOperation:operation];
[operation release];
}
- (void) loadRecords:(NSArray *)data {
NSLog(@"Runing sql");
NSString * sql = [data objectAtIndex:0];
Class cls = [data objectAtIndex:1];
Db *db= [Db currentDb];
NSArray *list = [db loadAndFill:sql theClass:cls];
[UIAppDelegate performSelectorOnMainThread:@selector(recordsLoaded:)
withObject:list
waitUntilDone:YES];
}
- (void) recordsLoaded:(NSArray *)data {
NSLog(@"Loading sql");
for (DbObject *o in data) {
[self.objectCache setObject:o forKey:[NSNumber numberWithInt:o.Id]];
}
}
However, I get a error when try to run
[UIAppDelegate performSelectorOnMainThread:@selector(recordsLoaded:)
withObject:list
waitUntilDone:YES];
And complain about not can send a message to recordsLoaded.
This is the correct aproach?
And how fill the data in the UITableView?