gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:44:44 27 4
gpt4 key购买 nike

我正在使用搜索栏从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];
}


}
}

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

提前致谢

最佳答案

您无法在辅助线程中更新UI,如果您需要更新UI,请使用-

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

关于objective-c - 由于辅助线程尝试更新UI而导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5893860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com