Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS/macOS backgrounding fix #769

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# define DLog(...)
#endif

#if !__has_feature(objc_arc)
# error "Missing objc_arc feature"
#endif

@implementation SQLitePlugin

@synthesize openDBs;
Expand All @@ -31,10 +35,6 @@ -(void)pluginInitialize
{
openDBs = [PSPDFThreadSafeMutableDictionary dictionaryWithCapacity:0];
appDBPaths = [NSMutableDictionary dictionaryWithCapacity:0];
#if !__has_feature(objc_arc)
[openDBs retain];
[appDBPaths retain];
#endif

NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
DLog(@"Detected docs path: %@", docs);
Expand Down Expand Up @@ -268,7 +268,7 @@ -(void) executeSqlBatchNow: (CDVInvokedUrlCommand*)command

CDVPluginResult* pluginResult;

@synchronized(self) {
{
for (NSMutableDictionary *dict in executes) {
CDVPluginResult *result = [self executeSqlWithDict:dict andArgs:dbargs];
if ([result.status intValue] == CDVCommandStatus_ERROR) {
Expand All @@ -293,44 +293,35 @@ -(void) executeSqlBatchNow: (CDVInvokedUrlCommand*)command
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

-(void) backgroundExecuteSql: (CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
[self executeSql:command];
}];
}

-(void) executeSql: (CDVInvokedUrlCommand*)command
{
NSMutableDictionary *options = [command.arguments objectAtIndex:0];
NSMutableDictionary *dbargs = [options objectForKey:@"dbargs"];
NSMutableDictionary *ex = [options objectForKey:@"ex"];

CDVPluginResult* pluginResult;
@synchronized (self) {
pluginResult = [self executeSqlWithDict: ex andArgs: dbargs];
}
CDVPluginResult * pluginResult = [self executeSqlWithDict: ex andArgs: dbargs];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

-(CDVPluginResult*) executeSqlWithDict: (NSMutableDictionary*)options andArgs: (NSMutableDictionary*)dbargs
{
NSString *dbFileName = [dbargs objectForKey:@"dbname"];
if (dbFileName == NULL) {
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"You must specify database path"];
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INTERNAL ERROR: You must specify database path"];
}

NSMutableArray *params = [options objectForKey:@"params"]; // optional

NSValue *dbPointer = [openDBs objectForKey:dbFileName];
if (dbPointer == NULL) {
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No such database, you must open it first"];
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INTERNAL ERROR: No such database, you must open it first"];
}
sqlite3 *db = [dbPointer pointerValue];

NSString *sql = [options objectForKey:@"sql"];
if (sql == NULL) {
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"You must specify a sql query to execute"];
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INTERNAL ERROR: You must specify a sql query to execute"];
}

const char *sql_stmt = [sql UTF8String];
Expand Down Expand Up @@ -393,9 +384,6 @@ -(CDVPluginResult*) executeSqlWithDict: (NSMutableDictionary*)options andArgs: (
columnValue = [[NSString alloc] initWithBytes:(char *)sqlite3_column_text(statement, i)
length:sqlite3_column_bytes(statement, i)
encoding:NSUTF8StringEncoding];
#if !__has_feature(objc_arc)
[columnValue autorelease];
#endif
break;
case SQLITE_NULL:
// just in case (should not happen):
Expand Down Expand Up @@ -497,12 +485,6 @@ -(void)dealloc
db = [pointer pointerValue];
sqlite3_close (db);
}

#if !__has_feature(objc_arc)
[openDBs release];
[appDBPaths release];
[super dealloc];
#endif
}

+(NSDictionary *)captureSQLiteErrorFromDb:(struct sqlite3 *)db
Expand Down