I have use case where multiple threads perform CRUD operations on ios
couchbase-lite database. When i refer couchbase-lite developer documentation
<http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/database/index.html#concurrency-support>,
they suggested as below :
*If your app uses Couchbase-lite on multiple threads, then on each thread
(or dispatch queue) it must:*
- Create a new CBLManager instance. If you use multiple threads, do not
use the sharedInstance.
- Use only objects (Databases, Documents, ...) acquired from its Manager.
-
Not pass any Couchbase Lite objects to code running on any other
thread/queue*
I tried to create database on each thread and use it as per suggestion.
But failing to create database.*Getting database as nil.*
Can someone suggest where things are going wrong and why database is coming
as nil.
Below is my code for creating database :
+(CBLDatabase*)database {
CBLDatabase* database = [self getThreadLocalObjectForKey:@"CBLDatabase"];
if(!database) {
NSError* error = nil;
database = [[self getCBManager] databaseNamed:DATABASE_NAME
error:&error];
if(error || !database) {
//ERROR => Database is coming as nil
NSLog(@"serious problem occurred while opening CBL database with %@
and error => %@", DATABASE_NAME, error);
} else {
[self setThreadLocalObject:database forKey:@"CBLDatabase"];
}
}
return database;}
+(CBLManager*)getCBManager {
CBLManager* manager = [self getThreadLocalObjectForKey:@"CBLManager"];
if(!manager) {
// manager = [[CBLManager sharedInstance] copy];
NSError* error;
CBLManager *manager = [[CBLManager alloc] initWithDirectory:
CBLManager.defaultDirectory options:NULL error: &error];
if (error) {
NSLog(@"Cannot create Manager instance with custom options");
exit(-1);
}else{
[self setThreadLocalObject:manager forKey:@"CBLManager"];
}
}
return manager;}
+(id)getThreadLocalObjectForKey:(NSString*)key {
NSMutableDictionary* threadLocalObjects = [NSThread
currentThread].threadDictionary;
return threadLocalObjects[key];}
+(void)setThreadLocalObject:(id)object forKey:(NSString*)key {
NSMutableDictionary* threadLocalObjects = [NSThread
currentThread].threadDictionary;
threadLocalObjects[key] = object;}
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/9acfb076-3a5f-4853-9762-2836deb5aa28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.