在主队列中使用Sqlite和UI操作

时间:2017-04-06 12:52:24

标签: ios objective-c sqlite grand-central-dispatch dispatch-async

我需要帮助。我写app,应该保存数据,同时显示它们。但我有问题。这两个操作仅在主队列中工作。我可以做什么?谢谢!

我的用户界面代码

dispatch_async(dispatch_get_main_queue(), ^{
    [self.hostView.hostedGraph reloadData];
});

我的数据库代码

-(BOOL)addNewMesurable:(Mesurable*)mesurable{
    if (!_database) [self createDatabase];
    const char *charDBpath = [_dbPath UTF8String];
    if (sqlite3_open(charDBpath, &_database) == SQLITE_OK) {
        char* errorMessage;
        sqlite3_exec(_database, "BEGIN TRANSACTION", NULL, NULL, &errorMessage);

        NSString *query =
        [NSString stringWithFormat:@"insert into %@ (value , date, time)  values (? , ? , ? )",[Sensor sensorNameToString:
mesurable.sensor]];
        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            sqlite3_bind_text(statement, 1, [[mesurable.value stringValue] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 2, [[mesurable getStrindDate] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[mesurable getStrindTime] UTF8String], -1, NULL);
            //
            if(sqlite3_step(statement) != SQLITE_DONE){
                NSLog(@"ERROR add data from sensor - %@ ", [Sensor sensorNameToString: mesurable.sensor ]);

            }
            else NSLog(@"Done");
            sqlite3_clear_bindings(statement);
            sqlite3_reset(statement);
            sqlite3_exec(_database, "PRAGMA synchronous = OFF", NULL, NULL, &errorMessage);
            sqlite3_exec(_database, "PRAGMA journal_mode = MEMORY", NULL, NULL, &errorMessage);
        }
        sqlite3_exec(_database, "COMMIT TRANSACTION", NULL, NULL, &errorMessage);
        sqlite3_finalize(statement);
        sqlite3_close(_database);
    }

    return YES;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

您可以使用DISPATCH_QUEUE_PRIORITY_LOW,_HIGH和_DEFAULT

=TEXT(INDIRECT(A1), "")