簡體   English   中英

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

[英]Work with Sqlite and UI action in main queue

我需要幫助。 我編寫應用程序,該程序應保存數據並同時顯示它們。 但是我有問題。 兩種操作僅在主隊列中起作用。 我可以做什么? 謝謝!

我的UI代碼

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;
}

嘗試這個:

您可以使用DISPATCH_QUEUE_PRIORITY_LOW,_HIGH和_DEFAULT

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // now send the result back to the main thread so we can do
    // UIKit stuff. u can update ur ui  
    dispatch_async(dispatch_get_main_queue(), ^{
        // update your DB content
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM