I have some problems to make my queries in the background.
I download a JSON from internet, parse it with Reskit and save an array of objects with FMDB into my sqlite3 database encrypted with SQLCipher.
This is an example:
FMDatabaseQueue *_queueSelect = [FMDatabaseQueue databaseQueueWithPath:[DataBaseController getPathBaseDatos]];
[_queueSelect inDatabase:^(FMDatabase *db) {
[db setKey:DATABASE_KEY];
FMResultSet *existeConsulta = [db executeQuery:sql];
while([existeConsulta next]) {
[results addObject:[existeConsulta resultDictionary]];
}
}];
I tried to use "inTransaction" and "dispatch_async" but without success.
What is the right way to do this task?
To run this asynchronously, FMDatabaseQueue
doesn't do this asynchronous operation for you, but just gives you a framework so that when you interact with this queue from multiple threads, the interaction will be synchronized for you.
So, one would (a) instantiate a single FMDatabaseQueue
object for the entire app; and (b) when you want to run something asynchronously, you have to run that from a background dispatch or operation queue.
For example:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
[_queueSelect inDatabase:^(FMDatabase *db) {
// do your stuff here
}];
});