Manos Chorianopoulos

possible fix for db crashes, queue queries

......@@ -81,6 +81,7 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError *
@property (nonatomic, strong) AFHTTPSessionManager *httpClient;
@property (nonatomic) AFNetworkReachabilityStatus networkStatus;
@property (nonatomic) BOOL hasChangedReachabilityStatus;
@property (nonatomic, retain) NSString *DatabaseLock;
- (id)initWithApplicationUUID:(NSString *)appUUID;
- (BOOL)isRegistrationValid;
......@@ -284,6 +285,8 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError *
[defaults synchronize];
_pendingOperationsQueue = [[NSOperationQueue alloc] init];
_pendingOperationsQueue.maxConcurrentOperationCount = 1;
_DatabaseLock = @"Database-Lock";
}
return self;
}
......@@ -560,8 +563,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
[self.locationManager applicationDidEnterBackground];
[WLAnalyticsManager logAppDidEnterBackgroundEvent];
[self calculateNetworkReachabilityStatisticsForStatus:self.networkStatus];
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"PRAGMA shrink_memory"];
WL_FMDBLogError
}
}
#pragma mark - Inbox
......@@ -702,11 +707,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *accessToken = @"";
if ([_db tableExists:@"requestVariables"] == YES) {
@try {
@synchronized (_DatabaseLock) {
FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
while ([accessTokenSet next]) {
accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT accessToken error: %@", exception.reason);
accessToken = @"";
......@@ -729,11 +736,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *refreshToken = @"";
if ([_db tableExists:@"requestVariables"] == YES) {
@try {
@synchronized (_DatabaseLock) {
FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
while ([refreshTokenSet next]) {
refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT refreshToken error: %@", exception.reason);
refreshToken = @"";
......@@ -756,11 +765,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *clientId = @"";
if ([_db tableExists:@"requestVariables"] == YES) {
@try {
@synchronized (_DatabaseLock) {
FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
while ([clientIdSet next]) {
clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT clientId error: %@", exception.reason);
clientId = @"";
......@@ -783,11 +794,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *clientSecret = @"";
if ([_db tableExists:@"requestVariables"] == YES) {
@try {
@synchronized (_DatabaseLock) {
FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
while ([clientSecretSet next]) {
clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT clientSecret error: %@", exception.reason);
clientSecret = @"";
......@@ -945,15 +958,26 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
refreshToken = [contextResponse objectForKey:@"refresh_token"];
NSString* accessToken = [NSString alloc];
accessToken = [contextResponse objectForKey:@"access_oken"];
// @synchronized (_DatabaseLock) {
//
// }
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
if ([_db tableExists:@"requestVariables"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE requestVariables (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, client_id INTEGER, client_secret VARCHAR(255), access_token VARCHAR(255), refresh_token VARCHAR(255))"];
WL_FMDBLogError
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, refresh_token, access_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, refreshToken, accessToken];
}
} else {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ?, refresh_token = ?, access_token = ? WHERE id = 1", clientId, clientSecret, refreshToken, accessToken];
}
}
success(contextResponse);
}
} failureBlock:^(NSError *error) {
......@@ -1013,7 +1037,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
- (NSDictionary*)logout
{
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
NSLog(@"**************** WARPLY Logout *****************" );
return successResponse;
......@@ -1026,15 +1052,23 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
clientId = [contextResponse objectForKey:@"client_id"];
NSString* clientSecret = [NSString alloc];
clientSecret = [contextResponse objectForKey:@"client_secret"];
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
if ([_db tableExists:@"requestVariables"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE requestVariables (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, client_id INTEGER, client_secret VARCHAR(255), access_token VARCHAR(255), refresh_token VARCHAR(255))"];
WL_FMDBLogError
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret) VALUES (1, ?, ?)", clientId, clientSecret];
}
} else {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ? WHERE id = 1", clientId, clientSecret];
}
}
NSDictionary *postDictionary2 = [NSDictionary alloc];
if ([loginType isEqual:@"email"]) {
postDictionary2 = @{@"app_id": [contextResponse objectForKey:@"app_id"], @"client_id": clientId, @"confirm": @"yes", @"email":id, @"response_type":@"code", @"scope": [loginType stringByAppendingString:@" app_id"] };
......@@ -1064,7 +1098,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSData *jsonData3 = [NSJSONSerialization dataWithJSONObject:postDictionary3 options:0 error:NULL];
[self sendContext5:jsonData3 successBlock:^(NSDictionary *contextResponse) {
if (success) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
success(successResponse);
}
......@@ -1123,8 +1159,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
- (void)updateRefreshTokenW:(NSString*)access_token :(NSString*)refresh_token
{
if ([_db tableExists:@"requestVariables"] == YES) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", access_token, refresh_token];
}
}
}
- (void)refreshToken:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
......@@ -1135,17 +1173,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *clientSecret = @"";
@try {
@synchronized (_DatabaseLock) {
FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
// NSString *refreshToken = @"";
while ([refreshTokenSet next]) {
refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
// NSString *clientId = [NSString alloc];
// NSString *clientId = @"";
while ([clientIdSet next]) {
clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
// NSString *clientSecret = [NSString alloc];
// NSString *clientSecret = @"";
......@@ -1153,6 +1196,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT Token error: %@", exception.reason);
if (failure) {
......@@ -1174,7 +1218,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
// success(successResponse);
@try {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
success(successResponse);
}
......@@ -1223,17 +1269,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *clientSecret = @"";
@try {
@synchronized (_DatabaseLock) {
FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
// NSString *refreshToken = @"";
while ([refreshTokenSet next]) {
refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
// NSString *clientId = [NSString alloc];
// NSString *clientId = @"";
while ([clientIdSet next]) {
clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
// NSString *clientSecret = [NSString alloc];
// NSString *clientSecret = @"";
......@@ -1241,6 +1292,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT Token error: %@", exception.reason);
if (failure) {
......@@ -1261,7 +1313,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
// success(successResponse);
@try {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
success(successResponse);
}
......@@ -1310,17 +1364,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
NSString *clientSecret = @"";
@try {
@synchronized (_DatabaseLock) {
FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
// NSString *refreshToken = @"";
while ([refreshTokenSet next]) {
refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
// NSString *clientId = [NSString alloc];
// NSString *clientId = @"";
while ([clientIdSet next]) {
clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
}
}
@synchronized (_DatabaseLock) {
FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
// NSString *clientSecret = [NSString alloc];
// NSString *clientSecret = @"";
......@@ -1328,6 +1387,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT Token error: %@", exception.reason);
if (failure) {
......@@ -1348,7 +1408,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
// success(successResponse);
@try {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
success(successResponse);
}
......@@ -1376,8 +1438,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
// } failureBlock:^(NSError *error) {
if (failure) {
if ([_db tableExists:@"requestVariables"] == YES) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
}
failure(error);
}
NSLog(@"Error at token %@", error );
......@@ -1997,11 +2061,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
accessToken = [tokens objectForKey:@"access_token"];
NSString* clientSecret = [NSString alloc];
clientSecret = [tokens objectForKey:@"client_secret"];
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE requestVariables (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, client_id INTEGER, client_secret VARCHAR(255), access_token VARCHAR(255), refresh_token VARCHAR(255))"];
WL_FMDBLogError
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken];
}
NSNumber* status = [NSNumber alloc];
status = [contextResponse objectForKey:@"result"];
......@@ -2038,11 +2108,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
accessToken = [tokens objectForKey:@"access_token"];
NSString* clientSecret = [NSString alloc];
clientSecret = [tokens objectForKey:@"client_secret"];
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE requestVariables (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, client_id INTEGER, client_secret VARCHAR(255), access_token VARCHAR(255), refresh_token VARCHAR(255))"];
WL_FMDBLogError
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken];
}
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
if (success) {
......@@ -3654,7 +3730,9 @@ return appIsRegisteredWithWarply;
- (void)addEvent:(WLEvent *)event priority:(BOOL)priority
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event.data options:0 error:NULL];
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"INSERT INTO events (_id, type, time, data, priority) VALUES (NULL, ?, ?, ?, ?)", [event getType], event.time, jsonData, [NSNumber numberWithBool:priority]];
}
[self sendEventsIfNeeded];
}
......@@ -3710,7 +3788,9 @@ return appIsRegisteredWithWarply;
[self sendContext2:jsonAnalytics path:@"analytics" successBlock:^(NSDictionary *contextResponse) {
if(deleteSql){
NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
@synchronized (_DatabaseLock) {
[_db executeUpdate:queryString];
}
if ([_db hadError]) {
NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
......@@ -3739,7 +3819,9 @@ return appIsRegisteredWithWarply;
[self sendContext2:jsonDevice path:@"deviceInfo" successBlock:^(NSDictionary *contextResponse) {
if(deleteSql){
NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
@synchronized (_DatabaseLock) {
[_db executeUpdate:queryString];
}
if ([_db hadError]) {
NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
......@@ -3768,7 +3850,9 @@ return appIsRegisteredWithWarply;
[self sendContext2:jsonOther path:nil successBlock:^(NSDictionary *contextResponse) {
if(deleteSql){
NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
@synchronized (_DatabaseLock) {
[_db executeUpdate:queryString];
}
if ([_db hadError]) {
NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
......@@ -3816,6 +3900,7 @@ return appIsRegisteredWithWarply;
-(BOOL)checkIfUserLoactionIsInPois:(CLLocation *) location {
// NSString *locationQuery = [NSString stringWithFormat:@"SELECT * FROM (SELECT distance(lat, lon, %@, %@) as distance, * FROM pois) WHERE distance < pois.radius", [NSNumber numberWithDouble:location.coordinate.latitude], [NSNumber numberWithDouble:location.coordinate.longitude]];
@synchronized (_DatabaseLock) {
FMResultSet *result = [_db executeQuery:@"SELECT * FROM pois"];
if (result == nil) return false;
......@@ -3831,6 +3916,7 @@ return appIsRegisteredWithWarply;
return true;
}
}
}
return false;
}
......@@ -3852,8 +3938,10 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
// _db remove
for (NSDictionary *poisDict in [contextResponse valueForKeyPath:@"MAPP_GEOFENCING"]) {
NSString *query = [NSString stringWithFormat: @"INSERT INTO pois (id, lat, lon, radius) VALUES (%@, %@, %@, %@)", [poisDict valueForKeyPath:@"id"], [poisDict valueForKeyPath:@"lat"], [poisDict valueForKeyPath:@"long"], [poisDict valueForKeyPath:@"radius"]];
@synchronized (_DatabaseLock) {
[_db executeUpdate: query];
}
}
// [[self locationManager] sendLocation:[self locationManager].locationManager.location];
}
......@@ -4696,11 +4784,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
// accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
// }
@try {
@synchronized (_DatabaseLock) {
FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
while ([accessTokenSet next]) {
accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT accessToken error: %@", exception.reason);
if (failureBlock) {
......@@ -4851,11 +4941,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
NSString *accessToken = @"";
if ([_db tableExists:@"requestVariables"] == YES) {
@try {
@synchronized (_DatabaseLock) {
FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
while ([accessTokenSet next]) {
accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
}
}
}
@catch (NSException *exception) {
NSLog(@"SELECT accessToken error: %@", exception.reason);
if (failureBlock) {
......@@ -6461,23 +6553,32 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
{
if ([_db tableExists:@"events"] == YES && [_db tableExists:@"netstats"] == YES) {
if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"];
WL_FMDBLogError
}
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"VACUUM"];
WL_FMDBLogError
}
return;
}
if ([_db tableExists:@"events"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE events (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, type VARCHAR(255), time VARCHAR(255), data BLOB, priority INTEGER)"];
WL_FMDBLogError
}
} else if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"];
WL_FMDBLogError
}
}
// if ([_db tableExists:@"netstats"] == NO) {
// [_db executeUpdate:@"CREATE TABLE netstats (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, wifi INTEGER, wwan INTEGER, overall INTEGER, wifistart_timestamp INTEGER, wifiend_timestamp INTEGER, wifi_totaltime INTEGER, wifi_dirty INTEGER, wwanstart_timestamp INTEGER, wwanend_timestamp INTEGER, wwan_totaltime INTEGER, wwan_dirty INTEGER)"];
......@@ -6486,8 +6587,10 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
// }
if ([_db tableExists:@"pois"] == NO) {
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"CREATE TABLE pois (id INTEGER PRIMARY KEY NOT NULL UNIQUE, lat NUMERIC, lon NUMERIC, radius NUMERIC)"];
}
}
[_db makeFunctionNamed:@"DISTANCE" maximumArguments:4 withBlock:^(sqlite3_context *context, int argc, sqlite3_value **argv) {
distanceFunc(context, argc, argv);
......@@ -6498,10 +6601,14 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
* both behaviour and performance.
* http://sqlite.org/pragma.html
*/
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"PRAGMA auto_vacuum = FULL"];
WL_FMDBLogError
}
@synchronized (_DatabaseLock) {
[_db executeUpdate:@"PRAGMA synchronous = NORMAL"];
WL_FMDBLogError
}
}
#pragma mark SQLite functions
......@@ -6539,12 +6646,15 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
///////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldSendEvents
{
@synchronized (_DatabaseLock) {
FMResultSet *priorityEvents = [_db executeQuery:@"SELECT COUNT(*) FROM events WHERE priority = 1;"];
while ([priorityEvents next]) {
if ([priorityEvents intForColumnIndex:0] > 0) {
return YES;
}
}
}
@synchronized (_DatabaseLock) {
FMResultSet *rs = [_db executeQuery:@"SELECT COUNT(*) FROM events;"];
while ([rs next]) {
......@@ -6553,6 +6663,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
return YES;
}
}
}
return NO;
}
......@@ -6560,6 +6671,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
- (NSMutableArray *)topEventDictsInDB
{
NSMutableArray *events=[NSMutableArray array];
@synchronized (_DatabaseLock) {
FMResultSet *rs = [_db executeQueryWithFormat:@"SELECT * FROM events ORDER BY _id LIMIT %d", kEventsPerBulk];
while ([rs next]) {
NSData *data = [rs dataForColumn:@"data"];
......@@ -6568,6 +6680,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
[mutableEventDict setObject:[rs objectForColumnName:@"_id"] forKey:@"_id"];
[events addObject:mutableEventDict];
}
}
return events;
}
......