开发者

Crashing application due to secondary thread trying to update UI

I am using a searchbar for selecting the records from table that is populated by Sqlite DB. Upending the table in every 120 records and reloading the table in every 200 records. My apps is crashing when i am dragging the table to a limit.When certain limit is covered Its crashing with error "secondary thread trying to update UI".here is the code

 if (indexPath.row > limit)
    {
        llimit = llimit+200 ;
        ulimit = ulimit+200 ;
           NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [opq cancelAllOperations];
        NSLog(@"before ns operation");
        opq = [NSOperationQueue new];
        NSInvocationOperation *op = [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchData) object:nil] autorelease];
        [opq addOperation:op];
        [pool drain];
        i++;
        limit = limit + 120 ;
    }



- (void) searchData {

    NSString *databaseName = @"imeating.sql";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDir=[documentPaths objectAtIndex:0];
    NSString *databasePath=[documentsDir stringByAppendingPathComponent:databaseName];
    sqlite3 *database;

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        // Setup the SQL Statement and compile it for faster access
        sqlite3_stmt *compiledStatement ;
        const char *sqlStatement ;

        sqlStatement = "select category_id, upper(subitem_name), subitem_detail_id from subitem_detail where subitem_name LIKE ? order by subitem_name limit ?,?" ;
       NSLog(@"inside search b4 wildsearch %@",searchString);

        wildSearch = [NSString stringWithFormat:@"%@%@",searchString, @"%"];



        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {

            sqlite3_bind_text(compiledStatement, 1, [wildSearch UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(compiledStatement, 2, llimit);
            sqlite3_bind_int(compiledStatement, 3, ulimit);

            if (llimit <200){
                 NSLog(@"with in if limit < 200");

                itemArray = [[NSMutableArray alloc] init] ;
            }

            while (sqlite3_step(compiledStatement) == SQLITE_ROW) {


                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;

                NSString *categoryId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *itemName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *itemId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                if (ulimit%200 == 0)
                {

                    [newTable开发者_运维百科View reloadData];


                }
                [pool drain];
            } 


        }
    }

This is happening in device only not in simulator also Lazy loading is occurring in table view . I'm am using sqllite DB (contains huge amount of data) for populating the table. I can drag the table to a certain range of limit after then application is crashing.Crashing limit is not constant.Please help me to implement this in perfect way

Thanks in advance


You can't update UI in a secondary thread, if you need to update UI then use -

[self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜