HI guys,
I've been struggling with answers to the right way of calling SQLite methods in the background...
I've tried numerous and even found one that seems to work but i know that its NOT the right way to do it..
Heres what I'm trying to accomplish:
1-Normally, in simple methods, the UI freezes (maybe for milliseconds) when tapping the button that sends SQLite calls until it completes and fills up the UITableView. As it sends in "performselectorinMainThread" I'm looking for a way to make sure that the method is independent of such calls, and thats why i've been trying the performSelectorInBackgroundThread.. and NSThread detach..
Technically, i've achieved this via NSThread detachNewThreadSelector
2- In such a scenario when the UI is not frozen, that makes buttons free to touch and execute, How can i avoid any clashes with NSThread and the UI.. specifically UITableView.. Heres an example of the condition..
Consider a UISearchBar function "TextDidChange"
Normally, addition of a new Character yields results in tableView and until the results REACH tableView, the buttons of the keypad remain in the "Touched" state. In other words, the UI freezes..
I tried doing this with [NSThread detachNewThreadSelector].. it works Ok if i type slowly, but if i type faster, the app crashes.
This makes me believe that my Threading method is completely flawed..
heres what I'm doing.
This is in app delegate, Its a common method for both Search and Normal calls based on CategoryID.
This is in the Main Controller that houses the tableView, accessed from AppDelegate.
Finally, the Culprit, heres the problem, this is How im calling this method after numerous tryouts, this seemed to work.
AND this is what happens when you TAP THE UIBUTTON:
This works just fine, the problem is when an avid user like myself (or someguy in the world), who types in too fast or is very impatient, there are times when the taps between buttons have very different corresponding SQL result rows that translates to very big difference between the tableView rows.
And so i get the error "Index beyond bounds".
Now, Either i should be able to CANCEL the previous NSThread call .. or override it somehow.. This question is not just limited to its usage in the search function. So i do need to know how to solve it..!
Any right direction is helpful please..
thanks for takin the time to read it, its as much pain as typing it.. sheesh!
I've been struggling with answers to the right way of calling SQLite methods in the background...
I've tried numerous and even found one that seems to work but i know that its NOT the right way to do it..
Heres what I'm trying to accomplish:
1-Normally, in simple methods, the UI freezes (maybe for milliseconds) when tapping the button that sends SQLite calls until it completes and fills up the UITableView. As it sends in "performselectorinMainThread" I'm looking for a way to make sure that the method is independent of such calls, and thats why i've been trying the performSelectorInBackgroundThread.. and NSThread detach..
Technically, i've achieved this via NSThread detachNewThreadSelector
2- In such a scenario when the UI is not frozen, that makes buttons free to touch and execute, How can i avoid any clashes with NSThread and the UI.. specifically UITableView.. Heres an example of the condition..
Consider a UISearchBar function "TextDidChange"
Normally, addition of a new Character yields results in tableView and until the results REACH tableView, the buttons of the keypad remain in the "Touched" state. In other words, the UI freezes..
I tried doing this with [NSThread detachNewThreadSelector].. it works Ok if i type slowly, but if i type faster, the app crashes.
This makes me believe that my Threading method is completely flawed..
heres what I'm doing.
This is in app delegate, Its a common method for both Search and Normal calls based on CategoryID.
Code:
- (void) loadTitlesOfCategory:(NSInteger )category_ID containingString:(NSString *)searchString animated:(BOOL)animated{
[currentTitlesArray removeAllObjects];
if(nil == database){
//start database here
[self initialiseDatabase];
}
sqlite3_stmt *query;
BOOL doSearch = (nil != searchString) && ![searchString isEqualToString:@""];
if(category_ID == 0) return;
//UniversalSearch Function
if(doSearch){
//NSLog(@"in search");
NSString *sql;
sql = [NSString stringWithFormat:@"SELECT ID, TITLES from Ttable where TITLES LIKE ?"];
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &load_search_titles_query, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: Failed to prepare load_titles_of_category_search_query: '%s'.", sqlite3_errmsg(database));
}
query = load_search_titles_query;
const char *search_chars = [[NSString stringWithFormat:@"%%%@%%", searchString] UTF8String];
sqlite3_bind_text(query, 1, search_chars, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(query, 2, search_chars, -1, SQLITE_TRANSIENT);
}
else {
NSString *sql;
sql = [NSString stringWithFormat:@"SELECT ID, TITLES from Ttable where Category =?"];
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &load_titles_query, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: Failed to prepare load_titles_query: '%s'.", sqlite3_errmsg(database));
}
// }
query = load_titles_query;
sqlite3_bind_int(query, 1, category_ID);
}
NSMutableString *title = [NSMutableString string];
while(sqlite3_step(query) == SQLITE_ROW) {
int eid = sqlite3_column_int(query, 0);
char *titleTitle = (char *)sqlite3_column_text(query, 1);
TitleObj *title = [[title alloc] initWithID:eid title:titleTitle delegate:self];
[currenttitlesArray addObject:title];
[title release];
}
sqlite3_reset(query);
[self.mainController cacheArray:currenttitlesArray];
}
This is in the Main Controller that houses the tableView, accessed from AppDelegate.
Code:
-(- (void) cacheArray:(NSArray *)CurentArray{
self.currMainArray = CurentArray;
if(CurentArray){
}
[self.tableView reloadData];
}
Finally, the Culprit, heres the problem, this is How im calling this method after numerous tryouts, this seemed to work.
Code:
- (void)meth2:(NSNumber *)number {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CurrentCatID = [number integerValue];
[delegate loadTitlesOfCategory:CurrentCatID containingString:nil animated:YES];
[pool release];
}
AND this is what happens when you TAP THE UIBUTTON:
Code:
NSNumber *number = [NSNumber numberWithInteger:4];
[NSThread detachNewThreadSelector:@selector(meth2:) toTarget:self withObject:number];
This works just fine, the problem is when an avid user like myself (or someguy in the world), who types in too fast or is very impatient, there are times when the taps between buttons have very different corresponding SQL result rows that translates to very big difference between the tableView rows.
And so i get the error "Index beyond bounds".
Now, Either i should be able to CANCEL the previous NSThread call .. or override it somehow.. This question is not just limited to its usage in the search function. So i do need to know how to solve it..!
Any right direction is helpful please..
thanks for takin the time to read it, its as much pain as typing it.. sheesh!