由于辅助线程尝试更新UI而导致应用程序崩溃

时间:2011-05-05 07:02:22

标签: objective-c multithreading crash thread-safety

我正在使用搜索栏从Sqlite DB填充的表中选择记录。 在每120条记录中更新表,并在每200条记录中重新加载表。  当我将表拖动到一个限制时,我的应用程序崩溃。当某个限制被覆盖时它崩溃,错误“辅助线程试图更新UI”。这是代码

 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)
                {

                    [newTableView reloadData];


                }
                [pool drain];
            } 


        }
    }

仅在设备中发生这种情况,而不是在模拟器中也在表视图中发生延迟加载。我正在使用sqllite DB(包含大量数据)来填充表。 应用程序崩溃后,我可以将表拖动到一定范围的限制。崩溃限制不是恒定的。请帮助我以完美的方式实现这一点

提前致谢

1 个答案:

答案 0 :(得分:3)

如果您需要更新UI然后使用 -

,则无法在辅助线程中更新UI
[self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:YES];