Skip to content

Commit 6782561

Browse files
committed
refactor(sqlite): add possible failure handling around sqlite multithreading issues
1 parent f37ad02 commit 6782561

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

Leanplum-SDK/Classes/LPDatabase.m

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ - (id)init
5050
- (sqlite3 *)initSQLite
5151
{
5252
const char *sqliteFilePath = [[LPDatabase sqliteFilePath] UTF8String];
53-
int result = sqlite3_open(sqliteFilePath, &sqlite);
53+
int result = sqlite3_open_v2(sqliteFilePath, &sqlite, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
5454
if (result != SQLITE_OK) {
5555
[self handleSQLiteError:@"SQLite fail to open" errorResult:result query:nil];
5656
return nil;
@@ -190,40 +190,45 @@ - (NSArray *)rowsFromQuery:(NSString *)query bindObjects:(NSArray *)objectsToBin
190190
}
191191

192192
@synchronized (self) {
193-
NSMutableArray *rows = [NSMutableArray new];
194-
sqlite3_stmt *statement = [self sqliteStatementFromQuery:query
195-
bindObjects:objectsToBind];
196-
if (!statement) {
197-
return @[];
198-
}
199-
200-
// Iterate through rows.
201-
while (sqlite3_step(statement) == SQLITE_ROW) {
202-
// Get column data as dictionary where column name is the key
203-
// and value will be a blob or a string. This is a safe conversion.
204-
// Details: http://www.sqlite.org/c3ref/column_blob.html
205-
NSMutableDictionary *columnData = [NSMutableDictionary new];
206-
int columnsCount = sqlite3_column_count(statement);
207-
for (int i=0; i<columnsCount; i++){
208-
char *columnKeyUTF8 = (char *)sqlite3_column_name(statement, i);
209-
NSString *columnKey = [NSString stringWithUTF8String:columnKeyUTF8];
210-
211-
if (sqlite3_column_type(statement, i) == SQLITE_BLOB) {
212-
NSData *columnBytes = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, i)
213-
length:sqlite3_column_bytes(statement, i)];
214-
columnData[columnKey] = [NSKeyedUnarchiver unarchiveObjectWithData:columnBytes];
215-
} else {
216-
char *columnValueUTF8 = (char *)sqlite3_column_text(statement, i);
217-
if (columnValueUTF8) {
218-
NSString *columnValue = [NSString stringWithUTF8String:columnValueUTF8];
219-
columnData[columnKey] = columnValue;
193+
@try {
194+
NSMutableArray *rows = [NSMutableArray new];
195+
sqlite3_stmt *statement = [self sqliteStatementFromQuery:query
196+
bindObjects:objectsToBind];
197+
if (!statement) {
198+
return @[];
199+
}
200+
201+
// Iterate through rows.
202+
while (sqlite3_step(statement) == SQLITE_ROW) {
203+
// Get column data as dictionary where column name is the key
204+
// and value will be a blob or a string. This is a safe conversion.
205+
// Details: http://www.sqlite.org/c3ref/column_blob.html
206+
NSMutableDictionary *columnData = [NSMutableDictionary new];
207+
int columnsCount = sqlite3_column_count(statement);
208+
for (int i=0; i<columnsCount; i++){
209+
char *columnKeyUTF8 = (char *)sqlite3_column_name(statement, i);
210+
NSString *columnKey = [NSString stringWithUTF8String:columnKeyUTF8];
211+
212+
if (sqlite3_column_type(statement, i) == SQLITE_BLOB) {
213+
NSData *columnBytes = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, i)
214+
length:sqlite3_column_bytes(statement, i)];
215+
columnData[columnKey] = [NSKeyedUnarchiver unarchiveObjectWithData:columnBytes];
216+
} else {
217+
char *columnValueUTF8 = (char *)sqlite3_column_text(statement, i);
218+
if (columnValueUTF8) {
219+
NSString *columnValue = [NSString stringWithUTF8String:columnValueUTF8];
220+
columnData[columnKey] = columnValue;
221+
}
220222
}
221223
}
224+
[rows addObject:columnData];
222225
}
223-
[rows addObject:columnData];
226+
sqlite3_finalize(statement);
227+
return rows;
228+
} @catch (NSException *e) {
229+
LPLog(LPError, @"SQLite operation failed.");
230+
// TODO: Make sure to catch this when new logging is in place,
224231
}
225-
sqlite3_finalize(statement);
226-
return rows;
227232
}
228233
return @[];
229234
}

0 commit comments

Comments
 (0)