diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index b273ef3..fad72a0 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -22,6 +22,8 @@ @implementation SQLitePlugin @synthesize openDBs; @synthesize appDBPaths; +@synthesize commandCache; +@synthesize processingCache; -(void)pluginInitialize { @@ -30,6 +32,8 @@ -(void)pluginInitialize { openDBs = [NSMutableDictionary dictionaryWithCapacity:0]; appDBPaths = [NSMutableDictionary dictionaryWithCapacity:0]; + commandCache = [NSMutableArray arrayWithCapacity:0]; + processingCache = NO; #if !__has_feature(objc_arc) [openDBs retain]; [appDBPaths retain]; @@ -242,13 +246,33 @@ -(void)deleteNow: (CDVInvokedUrlCommand*)command } --(void) backgroundExecuteSqlBatch: (CDVInvokedUrlCommand*)command +-(void) processCache { + processingCache = YES; [self.commandDelegate runInBackground:^{ - [self executeSqlBatchNow: command]; + @synchronized (self) { + if (commandCache.count) { + CDVInvokedUrlCommand* cachedCmd = [commandCache objectAtIndex:0]; + [commandCache removeObjectAtIndex:0]; + [self executeSqlBatchNow: cachedCmd]; + } + else { + processingCache = NO; + } + } }]; } + +-(void) backgroundExecuteSqlBatch: (CDVInvokedUrlCommand*)command +{ + @synchronized (self) { + [commandCache addObject:command]; + if (!processingCache) + [self processCache]; + } +} + -(void) executeSqlBatchNow: (CDVInvokedUrlCommand*)command { NSMutableDictionary *options = [command.arguments objectAtIndex:0]; @@ -283,6 +307,7 @@ -(void) executeSqlBatchNow: (CDVInvokedUrlCommand*)command } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self processCache]; } -(void) backgroundExecuteSql: (CDVInvokedUrlCommand*)command