NSInvocationOperation Problem does the thread holds the database connection with it while executing.... check this function +(void)executeAddGroup
NSString*)sql
I am trying to call two methods through NSOperation which will run in background but the app crashes in between....
Actually i am loading details at apps start up in respective tableview but it takes time to load so thought of using threads...(am using indexing in tableview and thus the following code)
Please ignore any syntax errors..
I am trying to call two methods through NSOperation which will run in background but the app crashes in between....
Actually i am loading details at apps start up in respective tableview but it takes time to load so thought of using threads...(am using indexing in tableview and thus the following code)
Please ignore any syntax errors..
Code:
static sqlite3 *database = nil;
static sqlite3_stmt *selectStmt = nil;
static sqlite3_stmt *selectThreadStmt = nil;
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSortedContactsList) object:nil];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector (showSortedFavContactsList) object:nil];
/* Add the operation to the queue */
[queue addOperation:operation1];
[queue addOperation:operation2];
[operation1 release];
[operation2 release];
[queue release];
Code:
+(void)showSortedContactsList{
NSMutableString *sqlnonalphnum=[NSMutableString stringWithFormat:@"Select S_id,S_name,S_state,S_city from Supplier where "];
NSLog(@"Dataload");
for (int i=65; i<91;i++) {
//NSMutableArray *tempArray=[[NSMutableArray alloc]init];
[sqlnonalphnum appendFormat:@"S_name not like '%c%%' and ",i];
NSString *sql = [NSString stringWithFormat:@"select S_id,S_name,S_state,S_city from Supplier where S_name like '%c%%'",i];
[Dictionary executeAddGroup:sql];
}
NSString *sqlnonan = [NSString stringWithFormat:@"%@%@",[sqlnonalphnum substringToIndex:[sqlnonalphnum length]-4],@"order by s_name"];
[Dictionary executeAddGroup:sqlnonan];
}
+(void)showSortedFavContactsList{
NSMutableString *sqlnonalphnum=[NSMutableString stringWithFormat:@"Select S_id,S_name,S_state,S_city from Supplier where S_fav=1 "];
for (int i=65; i<91;i++) {
//NSMutableArray *tempArray=[[NSMutableArray alloc]init];
[sqlnonalphnum appendFormat:@"S_name not like '%c%%' and ",i];
NSString *sql = [NSString stringWithFormat:@"select S_id,S_name,S_state,S_city from Supplier where S_fav=1 and S_name like '%c%%'",i];
[Dictionary executeAddGroupFav:sql];
}
[Dictionary executeAddGroupFav:[sqlnonalphnum substringToIndex:[sqlnonalphnum length]-4]];
}
+(void)executeAddGroup:(NSString*)sql{
const char *sqlchar = [sql UTF8String];
if(sqlite3_prepare_v2(database, sqlchar, -1, &selectThreadStmt, NULL) == SQLITE_OK) {
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
while(sqlite3_step(selectThreadStmt) == SQLITE_ROW) {
int sid = (int)sqlite3_column_int(selectThreadStmt, 0);
NSString *snm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectThreadStmt, 1)];
NSString *sstate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectThreadStmt, 2)];
NSString *scity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectThreadStmt, 3)];
SupplierQuickInfo *sqinfo = [[SupplierQuickInfo alloc]init];
sqinfo.ssid = sid;
sqinfo.sname = snm;
sqinfo.sstate=sstate;
sqinfo.scitysite=scity;
[tempArray addObject:sqinfo];
[sqinfo release];
}
[listOfContacts addObject:tempArray];
[tempArray release];
selectThreadStmt=nil;
}
}
+(void)executeAddGroupFav:(NSString*)sql{
//NSLog(@" Query %@",sql);
const char *sqlchar = [sql UTF8String];
if(sqlite3_prepare_v2(database, sqlchar, -1, &selectStmt, NULL) == SQLITE_OK) {
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
while(sqlite3_step(selectStmt) == SQLITE_ROW) {
int sid = (int)sqlite3_column_int(selectStmt, 0);
NSString *snm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 1)];
NSString *sstate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 2)];
NSString *scity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 3)];
SupplierQuickInfo *sqinfo = [[SupplierQuickInfo alloc]init];
sqinfo.ssid = sid;
sqinfo.sname = snm;
sqinfo.sstate=sstate;
sqinfo.scitysite=scity;
[tempArray addObject:sqinfo];
[sqinfo release];
}
[listOfFavContacts addObject:tempArray];
[tempArray release];
selectStmt=nil;
}
}
Last edited: