0

我需要在后台在表中插入数据。我NSTimer用于在 sqlite 数据库的表中插入数据。但是它在插入过程中冻结了应用程序。我也使用过NSThread,但它也产生了同样的问题。我正在共享我正在使用的代码:

[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(InsertDataInTable) userInfo:nil repeats:NO];

-(void)InsertDataInTempTable
{
        DatabaseManager *objDb=[[DatabaseManager alloc]init];
        [objDb setDelegate:self];
        for(int i=0;i<[arrayTempTableData count];i++)
        {
            [objDb savedata:[dictCurrentUrlInfo objectForKey:TempTableName] elements:[arrayTempTableData objectAtIndex:i]];
        }
}

#pragma mark -
#pragma mark Save Operation

-(void)savedata:(NSString *)into elements:(NSMutableDictionary *)dictInsert{
        NSString *sql_String = @"";
        if([into isEqualToString:TABLEFirstAidTemp])
        {
            sql_String = [NSString stringWithFormat:@"INSERT INTO %@ VALUES('%@', '%@', '%@')", into,[dictInsert objectForKey:@"ArticleNumber"],[dictInsert objectForKey:@"ArticleTitle"],[dictInsert objectForKey:@"ArticleCategory"]];
        }
        sqlite3_exec(sql_Database1, [sql_String UTF8String], nil, nil, nil);
}

请为我提供一个适当的解决方案来摆脱这个问题。我想在后台执行在表中插入数据。提前致谢。

4

1 回答 1

0

解决方案是使用 NSThread。也许你执行错了。

您的代码将如下所示

-(void)InsertDataInTempTable
{
       [NSThread detachNewThreadSelector:@selector(InsertingData) toTarget:self withObject:nil];
}

-(void)InsertingData
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        DatabaseManager *objDb=[[DatabaseManager alloc]init];
        [objDb setDelegate:self];
        for(int i=0;i<[arrayTempTableData count];i++)
        {
            [objDb savedata:[dictCurrentUrlInfo objectForKey:TempTableName] elements:[arrayTempTableData objectAtIndex:i]];
        }
        [pool release];
}

#pragma mark -
#pragma mark Save Operation

-(void)savedata:(NSString *)into elements:(NSMutableDictionary *)dictInsert{
        NSString *sql_String = @"";
        if([into isEqualToString:TABLEFirstAidTemp])
        {
            sql_String = [NSString stringWithFormat:@"INSERT INTO %@ VALUES('%@', '%@', '%@')", into,[dictInsert objectForKey:@"ArticleNumber"],[dictInsert objectForKey:@"ArticleTitle"],[dictInsert objectForKey:@"ArticleCategory"]];
        }
        sqlite3_exec(sql_Database1, [sql_String UTF8String], nil, nil, nil);
}
于 2012-08-20T14:13:00.013 回答