I am using FMDB in my iOS project as sqlite library.
What I am doind is to instantiate a FMDatabase object and perform the needed queries in my services. For example:
- (NSArray *)getPersons
{
FMDatabase *db = [self getDatabase];
[db open];
NSMutableArray *data = [[NSMutableArray alloc] init];
FMResultSet *rs = [db executeQuery:@"SELECT * FROM person"];
while ([rs next]) {
Person *person = [[Person alloc] init];
// populate person from result set
[data addObject:person];
}
[db close];
return [NSArray arrayWithArray:data];
}
But, recently I've been requested to call some web services and store the person picture in base 64 in my sqlite database because is needed to show the persons pictures when the user is offline.
So, when I get the WS response I have to iterate throught the array, fetch the person picture, convert it to base 64 and store it in my database.
Because this proccess (iterate ws result array, fetch every person image, convert it to base 64, store it in the sqlite database) is heavy, I've made in background:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save persons and persons picture in database
});
It works... but because the save persons block executed in background could take some time, every other query that I run while this block is being executed doesn't work because the database is locked.
I've been reading that it is supposed to use FMDatabaseQueue instead FMDatabase class. But I didn't find any example to return values (ie: a persons array) using FMDatabaseQueue.
On the other side, in the FMDatabaseQueue class reference ( http://ift.tt/1KEuh28 ) it says that the class methods are blocking, so it would block my UI until my save persons block is finished.
How can I avoid my database being locked while storing big ammounts of data?
Aucun commentaire:
Enregistrer un commentaire