mercredi 6 mai 2015

Use two FMDB queues on the same database

I believe that my use case is fairly common, but I could not find an authoritative answer for this.

I have a sync mechanism that runs in background and write data to my database. This sync can take a lot of time (I use FTS). For this I use a FMDatabaseQueue. When I want to read on the database, I use the same queue to make a query.

Now, when the sync process already queued a lot of transactions to the queue then the app wants to do a read, it has to wait for all the write transactions to finish before being able to do a query. The code might look like this:

FMDatabaseQueue *queue = [self getDatabaseQueue];

[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
    // Very slow process
    [db executeUpdate:@"UPDATE docs SET name = ?", "value..."];
}];

[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
    // Very slow process
    [db executeUpdate:@"UPDATE docs SET name = ?", "value..."];
}];

[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
    // Very slow process
    [db executeUpdate:@"UPDATE docs SET name = ?", "value..."];
}];

[queue inDatabase:^(FMDatabase *db) {
    FMResultSet *resultSet = [db executeQuery:@"SELECT name..."];
}];

I want to have the query results instantaneously (even if the sync is not done) and not wait to the UPDATE to be done.

Can I create two FMDatabaseQueues, one for the write queries and one for the read queries? What will happen if a read query starts right in the middle of a write transaction?

Aucun commentaire:

Enregistrer un commentaire