Manos Chorianopoulos

possible fix for db crashes, queue queries

...@@ -81,6 +81,7 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError * ...@@ -81,6 +81,7 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError *
81 @property (nonatomic, strong) AFHTTPSessionManager *httpClient; 81 @property (nonatomic, strong) AFHTTPSessionManager *httpClient;
82 @property (nonatomic) AFNetworkReachabilityStatus networkStatus; 82 @property (nonatomic) AFNetworkReachabilityStatus networkStatus;
83 @property (nonatomic) BOOL hasChangedReachabilityStatus; 83 @property (nonatomic) BOOL hasChangedReachabilityStatus;
84 +@property (nonatomic, retain) NSString *DatabaseLock;
84 85
85 - (id)initWithApplicationUUID:(NSString *)appUUID; 86 - (id)initWithApplicationUUID:(NSString *)appUUID;
86 - (BOOL)isRegistrationValid; 87 - (BOOL)isRegistrationValid;
...@@ -284,6 +285,8 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError * ...@@ -284,6 +285,8 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError *
284 [defaults synchronize]; 285 [defaults synchronize];
285 _pendingOperationsQueue = [[NSOperationQueue alloc] init]; 286 _pendingOperationsQueue = [[NSOperationQueue alloc] init];
286 _pendingOperationsQueue.maxConcurrentOperationCount = 1; 287 _pendingOperationsQueue.maxConcurrentOperationCount = 1;
288 +
289 + _DatabaseLock = @"Database-Lock";
287 } 290 }
288 return self; 291 return self;
289 } 292 }
...@@ -560,8 +563,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -560,8 +563,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
560 [self.locationManager applicationDidEnterBackground]; 563 [self.locationManager applicationDidEnterBackground];
561 [WLAnalyticsManager logAppDidEnterBackgroundEvent]; 564 [WLAnalyticsManager logAppDidEnterBackgroundEvent];
562 [self calculateNetworkReachabilityStatisticsForStatus:self.networkStatus]; 565 [self calculateNetworkReachabilityStatisticsForStatus:self.networkStatus];
566 + @synchronized (_DatabaseLock) {
563 [_db executeUpdate:@"PRAGMA shrink_memory"]; 567 [_db executeUpdate:@"PRAGMA shrink_memory"];
564 WL_FMDBLogError 568 WL_FMDBLogError
569 + }
565 } 570 }
566 571
567 #pragma mark - Inbox 572 #pragma mark - Inbox
...@@ -702,11 +707,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -702,11 +707,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
702 NSString *accessToken = @""; 707 NSString *accessToken = @"";
703 if ([_db tableExists:@"requestVariables"] == YES) { 708 if ([_db tableExists:@"requestVariables"] == YES) {
704 @try { 709 @try {
710 + @synchronized (_DatabaseLock) {
705 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"]; 711 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
706 while ([accessTokenSet next]) { 712 while ([accessTokenSet next]) {
707 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue]; 713 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
708 } 714 }
709 } 715 }
716 + }
710 @catch (NSException *exception) { 717 @catch (NSException *exception) {
711 NSLog(@"SELECT accessToken error: %@", exception.reason); 718 NSLog(@"SELECT accessToken error: %@", exception.reason);
712 accessToken = @""; 719 accessToken = @"";
...@@ -729,11 +736,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -729,11 +736,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
729 NSString *refreshToken = @""; 736 NSString *refreshToken = @"";
730 if ([_db tableExists:@"requestVariables"] == YES) { 737 if ([_db tableExists:@"requestVariables"] == YES) {
731 @try { 738 @try {
739 + @synchronized (_DatabaseLock) {
732 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"]; 740 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
733 while ([refreshTokenSet next]) { 741 while ([refreshTokenSet next]) {
734 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue]; 742 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
735 } 743 }
736 } 744 }
745 + }
737 @catch (NSException *exception) { 746 @catch (NSException *exception) {
738 NSLog(@"SELECT refreshToken error: %@", exception.reason); 747 NSLog(@"SELECT refreshToken error: %@", exception.reason);
739 refreshToken = @""; 748 refreshToken = @"";
...@@ -756,11 +765,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -756,11 +765,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
756 NSString *clientId = @""; 765 NSString *clientId = @"";
757 if ([_db tableExists:@"requestVariables"] == YES) { 766 if ([_db tableExists:@"requestVariables"] == YES) {
758 @try { 767 @try {
768 + @synchronized (_DatabaseLock) {
759 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"]; 769 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
760 while ([clientIdSet next]) { 770 while ([clientIdSet next]) {
761 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue]; 771 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
762 } 772 }
763 } 773 }
774 + }
764 @catch (NSException *exception) { 775 @catch (NSException *exception) {
765 NSLog(@"SELECT clientId error: %@", exception.reason); 776 NSLog(@"SELECT clientId error: %@", exception.reason);
766 clientId = @""; 777 clientId = @"";
...@@ -783,11 +794,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -783,11 +794,13 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
783 NSString *clientSecret = @""; 794 NSString *clientSecret = @"";
784 if ([_db tableExists:@"requestVariables"] == YES) { 795 if ([_db tableExists:@"requestVariables"] == YES) {
785 @try { 796 @try {
797 + @synchronized (_DatabaseLock) {
786 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"]; 798 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
787 while ([clientSecretSet next]) { 799 while ([clientSecretSet next]) {
788 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue]; 800 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
789 } 801 }
790 } 802 }
803 + }
791 @catch (NSException *exception) { 804 @catch (NSException *exception) {
792 NSLog(@"SELECT clientSecret error: %@", exception.reason); 805 NSLog(@"SELECT clientSecret error: %@", exception.reason);
793 clientSecret = @""; 806 clientSecret = @"";
...@@ -945,15 +958,26 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -945,15 +958,26 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
945 refreshToken = [contextResponse objectForKey:@"refresh_token"]; 958 refreshToken = [contextResponse objectForKey:@"refresh_token"];
946 NSString* accessToken = [NSString alloc]; 959 NSString* accessToken = [NSString alloc];
947 accessToken = [contextResponse objectForKey:@"access_oken"]; 960 accessToken = [contextResponse objectForKey:@"access_oken"];
961 +// @synchronized (_DatabaseLock) {
962 +//
963 +// }
964 + @synchronized (_DatabaseLock) {
948 [_db executeUpdate:@"DROP TABLE requestVariables"]; 965 [_db executeUpdate:@"DROP TABLE requestVariables"];
966 + }
949 if ([_db tableExists:@"requestVariables"] == NO) { 967 if ([_db tableExists:@"requestVariables"] == NO) {
968 + @synchronized (_DatabaseLock) {
950 [_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))"]; 969 [_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))"];
951 WL_FMDBLogError 970 WL_FMDBLogError
971 + }
952 972
973 + @synchronized (_DatabaseLock) {
953 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, refresh_token, access_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, refreshToken, accessToken]; 974 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, refresh_token, access_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, refreshToken, accessToken];
975 + }
954 } else { 976 } else {
977 + @synchronized (_DatabaseLock) {
955 [_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ?, refresh_token = ?, access_token = ? WHERE id = 1", clientId, clientSecret, refreshToken, accessToken]; 978 [_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ?, refresh_token = ?, access_token = ? WHERE id = 1", clientId, clientSecret, refreshToken, accessToken];
956 } 979 }
980 + }
957 success(contextResponse); 981 success(contextResponse);
958 } 982 }
959 } failureBlock:^(NSError *error) { 983 } failureBlock:^(NSError *error) {
...@@ -1013,7 +1037,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1013,7 +1037,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1013 - (NSDictionary*)logout 1037 - (NSDictionary*)logout
1014 { 1038 {
1015 1039
1040 + @synchronized (_DatabaseLock) {
1016 [_db executeUpdate:@"DROP TABLE requestVariables"]; 1041 [_db executeUpdate:@"DROP TABLE requestVariables"];
1042 + }
1017 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 1043 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
1018 NSLog(@"**************** WARPLY Logout *****************" ); 1044 NSLog(@"**************** WARPLY Logout *****************" );
1019 return successResponse; 1045 return successResponse;
...@@ -1026,15 +1052,23 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1026,15 +1052,23 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1026 clientId = [contextResponse objectForKey:@"client_id"]; 1052 clientId = [contextResponse objectForKey:@"client_id"];
1027 NSString* clientSecret = [NSString alloc]; 1053 NSString* clientSecret = [NSString alloc];
1028 clientSecret = [contextResponse objectForKey:@"client_secret"]; 1054 clientSecret = [contextResponse objectForKey:@"client_secret"];
1055 + @synchronized (_DatabaseLock) {
1029 [_db executeUpdate:@"DROP TABLE requestVariables"]; 1056 [_db executeUpdate:@"DROP TABLE requestVariables"];
1057 + }
1030 if ([_db tableExists:@"requestVariables"] == NO) { 1058 if ([_db tableExists:@"requestVariables"] == NO) {
1059 + @synchronized (_DatabaseLock) {
1031 [_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))"]; 1060 [_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))"];
1032 WL_FMDBLogError 1061 WL_FMDBLogError
1062 + }
1033 1063
1064 + @synchronized (_DatabaseLock) {
1034 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret) VALUES (1, ?, ?)", clientId, clientSecret]; 1065 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret) VALUES (1, ?, ?)", clientId, clientSecret];
1066 + }
1035 } else { 1067 } else {
1068 + @synchronized (_DatabaseLock) {
1036 [_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ? WHERE id = 1", clientId, clientSecret]; 1069 [_db executeUpdate:@"UPDATE requestVariables SET client_id = ? , client_secret = ? WHERE id = 1", clientId, clientSecret];
1037 } 1070 }
1071 + }
1038 NSDictionary *postDictionary2 = [NSDictionary alloc]; 1072 NSDictionary *postDictionary2 = [NSDictionary alloc];
1039 if ([loginType isEqual:@"email"]) { 1073 if ([loginType isEqual:@"email"]) {
1040 postDictionary2 = @{@"app_id": [contextResponse objectForKey:@"app_id"], @"client_id": clientId, @"confirm": @"yes", @"email":id, @"response_type":@"code", @"scope": [loginType stringByAppendingString:@" app_id"] }; 1074 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) ...@@ -1064,7 +1098,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1064 NSData *jsonData3 = [NSJSONSerialization dataWithJSONObject:postDictionary3 options:0 error:NULL]; 1098 NSData *jsonData3 = [NSJSONSerialization dataWithJSONObject:postDictionary3 options:0 error:NULL];
1065 [self sendContext5:jsonData3 successBlock:^(NSDictionary *contextResponse) { 1099 [self sendContext5:jsonData3 successBlock:^(NSDictionary *contextResponse) {
1066 if (success) { 1100 if (success) {
1101 + @synchronized (_DatabaseLock) {
1067 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]]; 1102 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
1103 + }
1068 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 1104 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
1069 success(successResponse); 1105 success(successResponse);
1070 } 1106 }
...@@ -1123,8 +1159,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1123,8 +1159,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1123 - (void)updateRefreshTokenW:(NSString*)access_token :(NSString*)refresh_token 1159 - (void)updateRefreshTokenW:(NSString*)access_token :(NSString*)refresh_token
1124 { 1160 {
1125 if ([_db tableExists:@"requestVariables"] == YES) { 1161 if ([_db tableExists:@"requestVariables"] == YES) {
1162 + @synchronized (_DatabaseLock) {
1126 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", access_token, refresh_token]; 1163 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", access_token, refresh_token];
1127 } 1164 }
1165 + }
1128 } 1166 }
1129 1167
1130 - (void)refreshToken:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure 1168 - (void)refreshToken:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
...@@ -1135,17 +1173,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1135,17 +1173,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1135 NSString *clientSecret = @""; 1173 NSString *clientSecret = @"";
1136 1174
1137 @try { 1175 @try {
1176 + @synchronized (_DatabaseLock) {
1138 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"]; 1177 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
1139 // NSString *refreshToken = @""; 1178 // NSString *refreshToken = @"";
1140 while ([refreshTokenSet next]) { 1179 while ([refreshTokenSet next]) {
1141 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue]; 1180 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
1142 } 1181 }
1182 + }
1183 + @synchronized (_DatabaseLock) {
1143 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"]; 1184 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
1144 // NSString *clientId = [NSString alloc]; 1185 // NSString *clientId = [NSString alloc];
1145 // NSString *clientId = @""; 1186 // NSString *clientId = @"";
1146 while ([clientIdSet next]) { 1187 while ([clientIdSet next]) {
1147 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue]; 1188 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
1148 } 1189 }
1190 + }
1191 + @synchronized (_DatabaseLock) {
1149 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"]; 1192 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
1150 // NSString *clientSecret = [NSString alloc]; 1193 // NSString *clientSecret = [NSString alloc];
1151 // NSString *clientSecret = @""; 1194 // NSString *clientSecret = @"";
...@@ -1153,6 +1196,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1153,6 +1196,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1153 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue]; 1196 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
1154 } 1197 }
1155 } 1198 }
1199 + }
1156 @catch (NSException *exception) { 1200 @catch (NSException *exception) {
1157 NSLog(@"SELECT Token error: %@", exception.reason); 1201 NSLog(@"SELECT Token error: %@", exception.reason);
1158 if (failure) { 1202 if (failure) {
...@@ -1174,7 +1218,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1174,7 +1218,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1174 // success(successResponse); 1218 // success(successResponse);
1175 1219
1176 @try { 1220 @try {
1221 + @synchronized (_DatabaseLock) {
1177 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]]; 1222 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
1223 + }
1178 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 1224 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
1179 success(successResponse); 1225 success(successResponse);
1180 } 1226 }
...@@ -1223,17 +1269,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1223,17 +1269,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1223 NSString *clientSecret = @""; 1269 NSString *clientSecret = @"";
1224 1270
1225 @try { 1271 @try {
1272 + @synchronized (_DatabaseLock) {
1226 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"]; 1273 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
1227 // NSString *refreshToken = @""; 1274 // NSString *refreshToken = @"";
1228 while ([refreshTokenSet next]) { 1275 while ([refreshTokenSet next]) {
1229 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue]; 1276 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
1230 } 1277 }
1278 + }
1279 + @synchronized (_DatabaseLock) {
1231 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"]; 1280 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
1232 // NSString *clientId = [NSString alloc]; 1281 // NSString *clientId = [NSString alloc];
1233 // NSString *clientId = @""; 1282 // NSString *clientId = @"";
1234 while ([clientIdSet next]) { 1283 while ([clientIdSet next]) {
1235 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue]; 1284 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
1236 } 1285 }
1286 + }
1287 + @synchronized (_DatabaseLock) {
1237 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"]; 1288 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
1238 // NSString *clientSecret = [NSString alloc]; 1289 // NSString *clientSecret = [NSString alloc];
1239 // NSString *clientSecret = @""; 1290 // NSString *clientSecret = @"";
...@@ -1241,6 +1292,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1241,6 +1292,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1241 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue]; 1292 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
1242 } 1293 }
1243 } 1294 }
1295 + }
1244 @catch (NSException *exception) { 1296 @catch (NSException *exception) {
1245 NSLog(@"SELECT Token error: %@", exception.reason); 1297 NSLog(@"SELECT Token error: %@", exception.reason);
1246 if (failure) { 1298 if (failure) {
...@@ -1261,7 +1313,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1261,7 +1313,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1261 // success(successResponse); 1313 // success(successResponse);
1262 1314
1263 @try { 1315 @try {
1316 + @synchronized (_DatabaseLock) {
1264 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]]; 1317 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
1318 + }
1265 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 1319 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
1266 success(successResponse); 1320 success(successResponse);
1267 } 1321 }
...@@ -1310,17 +1364,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1310,17 +1364,22 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1310 NSString *clientSecret = @""; 1364 NSString *clientSecret = @"";
1311 1365
1312 @try { 1366 @try {
1367 + @synchronized (_DatabaseLock) {
1313 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"]; 1368 FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
1314 // NSString *refreshToken = @""; 1369 // NSString *refreshToken = @"";
1315 while ([refreshTokenSet next]) { 1370 while ([refreshTokenSet next]) {
1316 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue]; 1371 refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
1317 } 1372 }
1373 + }
1374 + @synchronized (_DatabaseLock) {
1318 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"]; 1375 FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
1319 // NSString *clientId = [NSString alloc]; 1376 // NSString *clientId = [NSString alloc];
1320 // NSString *clientId = @""; 1377 // NSString *clientId = @"";
1321 while ([clientIdSet next]) { 1378 while ([clientIdSet next]) {
1322 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue]; 1379 clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
1323 } 1380 }
1381 + }
1382 + @synchronized (_DatabaseLock) {
1324 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"]; 1383 FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
1325 // NSString *clientSecret = [NSString alloc]; 1384 // NSString *clientSecret = [NSString alloc];
1326 // NSString *clientSecret = @""; 1385 // NSString *clientSecret = @"";
...@@ -1328,6 +1387,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1328,6 +1387,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1328 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue]; 1387 clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
1329 } 1388 }
1330 } 1389 }
1390 + }
1331 @catch (NSException *exception) { 1391 @catch (NSException *exception) {
1332 NSLog(@"SELECT Token error: %@", exception.reason); 1392 NSLog(@"SELECT Token error: %@", exception.reason);
1333 if (failure) { 1393 if (failure) {
...@@ -1348,7 +1408,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1348,7 +1408,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1348 // success(successResponse); 1408 // success(successResponse);
1349 1409
1350 @try { 1410 @try {
1411 + @synchronized (_DatabaseLock) {
1351 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]]; 1412 [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
1413 + }
1352 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 1414 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
1353 success(successResponse); 1415 success(successResponse);
1354 } 1416 }
...@@ -1376,8 +1438,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1376,8 +1438,10 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1376 // } failureBlock:^(NSError *error) { 1438 // } failureBlock:^(NSError *error) {
1377 if (failure) { 1439 if (failure) {
1378 if ([_db tableExists:@"requestVariables"] == YES) { 1440 if ([_db tableExists:@"requestVariables"] == YES) {
1441 + @synchronized (_DatabaseLock) {
1379 [_db executeUpdate:@"DROP TABLE requestVariables"]; 1442 [_db executeUpdate:@"DROP TABLE requestVariables"];
1380 } 1443 }
1444 + }
1381 failure(error); 1445 failure(error);
1382 } 1446 }
1383 NSLog(@"Error at token %@", error ); 1447 NSLog(@"Error at token %@", error );
...@@ -1997,11 +2061,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -1997,11 +2061,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
1997 accessToken = [tokens objectForKey:@"access_token"]; 2061 accessToken = [tokens objectForKey:@"access_token"];
1998 NSString* clientSecret = [NSString alloc]; 2062 NSString* clientSecret = [NSString alloc];
1999 clientSecret = [tokens objectForKey:@"client_secret"]; 2063 clientSecret = [tokens objectForKey:@"client_secret"];
2064 + @synchronized (_DatabaseLock) {
2000 [_db executeUpdate:@"DROP TABLE requestVariables"]; 2065 [_db executeUpdate:@"DROP TABLE requestVariables"];
2066 + }
2067 + @synchronized (_DatabaseLock) {
2001 [_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))"]; 2068 [_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))"];
2002 WL_FMDBLogError 2069 WL_FMDBLogError
2070 + }
2003 2071
2072 + @synchronized (_DatabaseLock) {
2004 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken]; 2073 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken];
2074 + }
2005 2075
2006 NSNumber* status = [NSNumber alloc]; 2076 NSNumber* status = [NSNumber alloc];
2007 status = [contextResponse objectForKey:@"result"]; 2077 status = [contextResponse objectForKey:@"result"];
...@@ -2038,11 +2108,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -2038,11 +2108,17 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
2038 accessToken = [tokens objectForKey:@"access_token"]; 2108 accessToken = [tokens objectForKey:@"access_token"];
2039 NSString* clientSecret = [NSString alloc]; 2109 NSString* clientSecret = [NSString alloc];
2040 clientSecret = [tokens objectForKey:@"client_secret"]; 2110 clientSecret = [tokens objectForKey:@"client_secret"];
2111 + @synchronized (_DatabaseLock) {
2041 [_db executeUpdate:@"DROP TABLE requestVariables"]; 2112 [_db executeUpdate:@"DROP TABLE requestVariables"];
2113 + }
2114 + @synchronized (_DatabaseLock) {
2042 [_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))"]; 2115 [_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))"];
2043 WL_FMDBLogError 2116 WL_FMDBLogError
2117 + }
2044 2118
2119 + @synchronized (_DatabaseLock) {
2045 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken]; 2120 [_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken];
2121 + }
2046 2122
2047 NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 2123 NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
2048 if (success) { 2124 if (success) {
...@@ -3654,7 +3730,9 @@ return appIsRegisteredWithWarply; ...@@ -3654,7 +3730,9 @@ return appIsRegisteredWithWarply;
3654 - (void)addEvent:(WLEvent *)event priority:(BOOL)priority 3730 - (void)addEvent:(WLEvent *)event priority:(BOOL)priority
3655 { 3731 {
3656 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event.data options:0 error:NULL]; 3732 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event.data options:0 error:NULL];
3733 + @synchronized (_DatabaseLock) {
3657 [_db executeUpdate:@"INSERT INTO events (_id, type, time, data, priority) VALUES (NULL, ?, ?, ?, ?)", [event getType], event.time, jsonData, [NSNumber numberWithBool:priority]]; 3734 [_db executeUpdate:@"INSERT INTO events (_id, type, time, data, priority) VALUES (NULL, ?, ?, ?, ?)", [event getType], event.time, jsonData, [NSNumber numberWithBool:priority]];
3735 + }
3658 [self sendEventsIfNeeded]; 3736 [self sendEventsIfNeeded];
3659 } 3737 }
3660 3738
...@@ -3710,7 +3788,9 @@ return appIsRegisteredWithWarply; ...@@ -3710,7 +3788,9 @@ return appIsRegisteredWithWarply;
3710 [self sendContext2:jsonAnalytics path:@"analytics" successBlock:^(NSDictionary *contextResponse) { 3788 [self sendContext2:jsonAnalytics path:@"analytics" successBlock:^(NSDictionary *contextResponse) {
3711 if(deleteSql){ 3789 if(deleteSql){
3712 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds]; 3790 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
3791 + @synchronized (_DatabaseLock) {
3713 [_db executeUpdate:queryString]; 3792 [_db executeUpdate:queryString];
3793 + }
3714 3794
3715 if ([_db hadError]) { 3795 if ([_db hadError]) {
3716 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]); 3796 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
...@@ -3739,7 +3819,9 @@ return appIsRegisteredWithWarply; ...@@ -3739,7 +3819,9 @@ return appIsRegisteredWithWarply;
3739 [self sendContext2:jsonDevice path:@"deviceInfo" successBlock:^(NSDictionary *contextResponse) { 3819 [self sendContext2:jsonDevice path:@"deviceInfo" successBlock:^(NSDictionary *contextResponse) {
3740 if(deleteSql){ 3820 if(deleteSql){
3741 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds]; 3821 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
3822 + @synchronized (_DatabaseLock) {
3742 [_db executeUpdate:queryString]; 3823 [_db executeUpdate:queryString];
3824 + }
3743 3825
3744 if ([_db hadError]) { 3826 if ([_db hadError]) {
3745 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]); 3827 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
...@@ -3768,7 +3850,9 @@ return appIsRegisteredWithWarply; ...@@ -3768,7 +3850,9 @@ return appIsRegisteredWithWarply;
3768 [self sendContext2:jsonOther path:nil successBlock:^(NSDictionary *contextResponse) { 3850 [self sendContext2:jsonOther path:nil successBlock:^(NSDictionary *contextResponse) {
3769 if(deleteSql){ 3851 if(deleteSql){
3770 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds]; 3852 NSString *queryString = [NSString stringWithFormat:@"DELETE FROM events WHERE _id IN (%@)", commaSeperatedIds];
3853 + @synchronized (_DatabaseLock) {
3771 [_db executeUpdate:queryString]; 3854 [_db executeUpdate:queryString];
3855 + }
3772 3856
3773 if ([_db hadError]) { 3857 if ([_db hadError]) {
3774 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]); 3858 NSLog(@"Err %d: %@", [_db lastErrorCode], [_db lastErrorMessage]);
...@@ -3816,6 +3900,7 @@ return appIsRegisteredWithWarply; ...@@ -3816,6 +3900,7 @@ return appIsRegisteredWithWarply;
3816 -(BOOL)checkIfUserLoactionIsInPois:(CLLocation *) location { 3900 -(BOOL)checkIfUserLoactionIsInPois:(CLLocation *) location {
3817 // 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]]; 3901 // 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]];
3818 3902
3903 + @synchronized (_DatabaseLock) {
3819 FMResultSet *result = [_db executeQuery:@"SELECT * FROM pois"]; 3904 FMResultSet *result = [_db executeQuery:@"SELECT * FROM pois"];
3820 3905
3821 if (result == nil) return false; 3906 if (result == nil) return false;
...@@ -3831,6 +3916,7 @@ return appIsRegisteredWithWarply; ...@@ -3831,6 +3916,7 @@ return appIsRegisteredWithWarply;
3831 return true; 3916 return true;
3832 } 3917 }
3833 } 3918 }
3919 + }
3834 3920
3835 return false; 3921 return false;
3836 } 3922 }
...@@ -3852,8 +3938,10 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -3852,8 +3938,10 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
3852 // _db remove 3938 // _db remove
3853 for (NSDictionary *poisDict in [contextResponse valueForKeyPath:@"MAPP_GEOFENCING"]) { 3939 for (NSDictionary *poisDict in [contextResponse valueForKeyPath:@"MAPP_GEOFENCING"]) {
3854 NSString *query = [NSString stringWithFormat: @"INSERT INTO pois (id, lat, lon, radius) VALUES (%@, %@, %@, %@)", [poisDict valueForKeyPath:@"id"], [poisDict valueForKeyPath:@"lat"], [poisDict valueForKeyPath:@"long"], [poisDict valueForKeyPath:@"radius"]]; 3940 NSString *query = [NSString stringWithFormat: @"INSERT INTO pois (id, lat, lon, radius) VALUES (%@, %@, %@, %@)", [poisDict valueForKeyPath:@"id"], [poisDict valueForKeyPath:@"lat"], [poisDict valueForKeyPath:@"long"], [poisDict valueForKeyPath:@"radius"]];
3941 + @synchronized (_DatabaseLock) {
3855 [_db executeUpdate: query]; 3942 [_db executeUpdate: query];
3856 } 3943 }
3944 + }
3857 3945
3858 // [[self locationManager] sendLocation:[self locationManager].locationManager.location]; 3946 // [[self locationManager] sendLocation:[self locationManager].locationManager.location];
3859 } 3947 }
...@@ -4696,11 +4784,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -4696,11 +4784,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
4696 // accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue]; 4784 // accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
4697 // } 4785 // }
4698 @try { 4786 @try {
4787 + @synchronized (_DatabaseLock) {
4699 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"]; 4788 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
4700 while ([accessTokenSet next]) { 4789 while ([accessTokenSet next]) {
4701 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue]; 4790 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
4702 } 4791 }
4703 } 4792 }
4793 + }
4704 @catch (NSException *exception) { 4794 @catch (NSException *exception) {
4705 NSLog(@"SELECT accessToken error: %@", exception.reason); 4795 NSLog(@"SELECT accessToken error: %@", exception.reason);
4706 if (failureBlock) { 4796 if (failureBlock) {
...@@ -4851,11 +4941,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -4851,11 +4941,13 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
4851 NSString *accessToken = @""; 4941 NSString *accessToken = @"";
4852 if ([_db tableExists:@"requestVariables"] == YES) { 4942 if ([_db tableExists:@"requestVariables"] == YES) {
4853 @try { 4943 @try {
4944 + @synchronized (_DatabaseLock) {
4854 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"]; 4945 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
4855 while ([accessTokenSet next]) { 4946 while ([accessTokenSet next]) {
4856 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue]; 4947 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
4857 } 4948 }
4858 } 4949 }
4950 + }
4859 @catch (NSException *exception) { 4951 @catch (NSException *exception) {
4860 NSLog(@"SELECT accessToken error: %@", exception.reason); 4952 NSLog(@"SELECT accessToken error: %@", exception.reason);
4861 if (failureBlock) { 4953 if (failureBlock) {
...@@ -6461,23 +6553,32 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -6461,23 +6553,32 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
6461 { 6553 {
6462 if ([_db tableExists:@"events"] == YES && [_db tableExists:@"netstats"] == YES) { 6554 if ([_db tableExists:@"events"] == YES && [_db tableExists:@"netstats"] == YES) {
6463 if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) { 6555 if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) {
6556 +
6557 + @synchronized (_DatabaseLock) {
6464 [_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"]; 6558 [_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"];
6465 WL_FMDBLogError 6559 WL_FMDBLogError
6466 } 6560 }
6561 + }
6467 6562
6563 + @synchronized (_DatabaseLock) {
6468 [_db executeUpdate:@"VACUUM"]; 6564 [_db executeUpdate:@"VACUUM"];
6469 WL_FMDBLogError 6565 WL_FMDBLogError
6566 + }
6470 6567
6471 return; 6568 return;
6472 } 6569 }
6473 6570
6474 if ([_db tableExists:@"events"] == NO) { 6571 if ([_db tableExists:@"events"] == NO) {
6572 + @synchronized (_DatabaseLock) {
6475 [_db executeUpdate:@"CREATE TABLE events (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, type VARCHAR(255), time VARCHAR(255), data BLOB, priority INTEGER)"]; 6573 [_db executeUpdate:@"CREATE TABLE events (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, type VARCHAR(255), time VARCHAR(255), data BLOB, priority INTEGER)"];
6476 WL_FMDBLogError 6574 WL_FMDBLogError
6575 + }
6477 } else if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) { 6576 } else if ([_db columnExists:@"priority" inTableWithName:@"events"] == NO) {
6577 + @synchronized (_DatabaseLock) {
6478 [_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"]; 6578 [_db executeUpdate:@"ALTER TABLE events ADD COLUMN priority INTEGER"];
6479 WL_FMDBLogError 6579 WL_FMDBLogError
6480 } 6580 }
6581 + }
6481 6582
6482 // if ([_db tableExists:@"netstats"] == NO) { 6583 // if ([_db tableExists:@"netstats"] == NO) {
6483 // [_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)"]; 6584 // [_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) ...@@ -6486,8 +6587,10 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
6486 // } 6587 // }
6487 6588
6488 if ([_db tableExists:@"pois"] == NO) { 6589 if ([_db tableExists:@"pois"] == NO) {
6590 + @synchronized (_DatabaseLock) {
6489 [_db executeUpdate:@"CREATE TABLE pois (id INTEGER PRIMARY KEY NOT NULL UNIQUE, lat NUMERIC, lon NUMERIC, radius NUMERIC)"]; 6591 [_db executeUpdate:@"CREATE TABLE pois (id INTEGER PRIMARY KEY NOT NULL UNIQUE, lat NUMERIC, lon NUMERIC, radius NUMERIC)"];
6490 } 6592 }
6593 + }
6491 6594
6492 [_db makeFunctionNamed:@"DISTANCE" maximumArguments:4 withBlock:^(sqlite3_context *context, int argc, sqlite3_value **argv) { 6595 [_db makeFunctionNamed:@"DISTANCE" maximumArguments:4 withBlock:^(sqlite3_context *context, int argc, sqlite3_value **argv) {
6493 distanceFunc(context, argc, argv); 6596 distanceFunc(context, argc, argv);
...@@ -6498,10 +6601,14 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -6498,10 +6601,14 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
6498 * both behaviour and performance. 6601 * both behaviour and performance.
6499 * http://sqlite.org/pragma.html 6602 * http://sqlite.org/pragma.html
6500 */ 6603 */
6604 + @synchronized (_DatabaseLock) {
6501 [_db executeUpdate:@"PRAGMA auto_vacuum = FULL"]; 6605 [_db executeUpdate:@"PRAGMA auto_vacuum = FULL"];
6502 WL_FMDBLogError 6606 WL_FMDBLogError
6607 + }
6608 + @synchronized (_DatabaseLock) {
6503 [_db executeUpdate:@"PRAGMA synchronous = NORMAL"]; 6609 [_db executeUpdate:@"PRAGMA synchronous = NORMAL"];
6504 WL_FMDBLogError 6610 WL_FMDBLogError
6611 + }
6505 } 6612 }
6506 6613
6507 #pragma mark SQLite functions 6614 #pragma mark SQLite functions
...@@ -6539,12 +6646,15 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg ...@@ -6539,12 +6646,15 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
6539 /////////////////////////////////////////////////////////////////////////////// 6646 ///////////////////////////////////////////////////////////////////////////////
6540 - (BOOL)shouldSendEvents 6647 - (BOOL)shouldSendEvents
6541 { 6648 {
6649 + @synchronized (_DatabaseLock) {
6542 FMResultSet *priorityEvents = [_db executeQuery:@"SELECT COUNT(*) FROM events WHERE priority = 1;"]; 6650 FMResultSet *priorityEvents = [_db executeQuery:@"SELECT COUNT(*) FROM events WHERE priority = 1;"];
6543 while ([priorityEvents next]) { 6651 while ([priorityEvents next]) {
6544 if ([priorityEvents intForColumnIndex:0] > 0) { 6652 if ([priorityEvents intForColumnIndex:0] > 0) {
6545 return YES; 6653 return YES;
6546 } 6654 }
6547 } 6655 }
6656 + }
6657 + @synchronized (_DatabaseLock) {
6548 FMResultSet *rs = [_db executeQuery:@"SELECT COUNT(*) FROM events;"]; 6658 FMResultSet *rs = [_db executeQuery:@"SELECT COUNT(*) FROM events;"];
6549 6659
6550 while ([rs next]) { 6660 while ([rs next]) {
...@@ -6553,6 +6663,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg ...@@ -6553,6 +6663,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
6553 return YES; 6663 return YES;
6554 } 6664 }
6555 } 6665 }
6666 + }
6556 return NO; 6667 return NO;
6557 } 6668 }
6558 6669
...@@ -6560,6 +6671,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg ...@@ -6560,6 +6671,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
6560 - (NSMutableArray *)topEventDictsInDB 6671 - (NSMutableArray *)topEventDictsInDB
6561 { 6672 {
6562 NSMutableArray *events=[NSMutableArray array]; 6673 NSMutableArray *events=[NSMutableArray array];
6674 + @synchronized (_DatabaseLock) {
6563 FMResultSet *rs = [_db executeQueryWithFormat:@"SELECT * FROM events ORDER BY _id LIMIT %d", kEventsPerBulk]; 6675 FMResultSet *rs = [_db executeQueryWithFormat:@"SELECT * FROM events ORDER BY _id LIMIT %d", kEventsPerBulk];
6564 while ([rs next]) { 6676 while ([rs next]) {
6565 NSData *data = [rs dataForColumn:@"data"]; 6677 NSData *data = [rs dataForColumn:@"data"];
...@@ -6568,6 +6680,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg ...@@ -6568,6 +6680,7 @@ static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **arg
6568 [mutableEventDict setObject:[rs objectForColumnName:@"_id"] forKey:@"_id"]; 6680 [mutableEventDict setObject:[rs objectForColumnName:@"_id"] forKey:@"_id"];
6569 [events addObject:mutableEventDict]; 6681 [events addObject:mutableEventDict];
6570 } 6682 }
6683 + }
6571 return events; 6684 return events;
6572 } 6685 }
6573 6686
......