I need help. I write app, which should save data and at the same time show they. But I has problem. Both operations was working only in main queue. What I can do? Thanks!
My UI code
dispatch_async(dispatch_get_main_queue(), ^{
[self.hostView.hostedGraph reloadData];
});
My DB code
-(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;
}
try this:
You can use DISPATCH_QUEUE_PRIORITY_LOW,_HIGH and _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
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.