FMDatabaseQueue

228 views
Skip to first unread message

Jojo Batista

unread,
Apr 8, 2013, 10:53:11 AM4/8/13
to fm...@googlegroups.com
Hi, i'm trying to use queues in a singleton that is called across multiple threads. But i can't seem to make it work.
I don't fully understand how to use it.
I have, 1 connection, shared between threads, and one dbase.
Is it ok to ask for the queue in a singleton, and then use it in another threads? If that's ok, is ok to use it like this?

FMDatabaseQueue* queue = [[FMManager sharedFMManager] getQueue];

 __block NSArray* arr = [NSArray array];

[queue inDatabase:^(FMDatabase *db) {

        FMResultSet* result = [db executeQuery:queryString];

        arr = [self createResponseFromQuery:result];

        [result close];

    }];

//Should i add a (while (!end))?

return arr;


And i've been using some helper methods in my singleton to access the dbase.

Should it work if i create this method in my singleton?

-(BOOL)deleteTable:(NSString*)table wherePrimaryKey:(NSString*)key is:(NSString*)keyValue

{

    FMDatabaseQueue* queue = [self getQueue];

    [queue inDatabase:^(FMDatabase *db) {

        [db executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", table, key], keyValue];

        if ([db hadError]) {

            NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);

        }

    }];    

    return YES;

}


Cause i keep getting problems with race conditions :S EXC_BAD_ACCESS , etc


Gus Mueller

unread,
Apr 8, 2013, 12:57:45 PM4/8/13
to fm...@googlegroups.com
On Apr 8, 2013, at 7:53 AM, Jojo Batista <nbat...@makingsense.com> wrote:

> Hi, i'm trying to use queues in a singleton that is called across multiple
> threads. But i can't seem to make it work.
> I don't fully understand how to use it.
> I have, 1 connection, shared between threads, and one dbase.
> Is it ok to ask for the queue in a singleton, and then use it in another
> threads? If that's ok, is ok to use it like this?

Your code seems alright at first glance. What's the stack trace for the crashes? Is it happening in FMDB?

-gus

Jojo Batista

unread,
Apr 8, 2013, 1:38:39 PM4/8/13
to fm...@googlegroups.com
Apparently i had minor issues in some methods (some where calling the same queue inside the queue, and another method was deprecated and was stepping on top of the new ones)
I'll let you know if i have another problem. Thanks for the super fast response! 
PS: Good job with FMDB.
Reply all
Reply to author
Forward
0 new messages