Skip to content

Commit 8cd8a89

Browse files
committed
Added locks to lastLocation to prevent race conditions
1 parent aff3287 commit 8cd8a89

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
03432CDC1EBD426A0071FC48 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03432CDB1EBD426A0071FC48 /* CoreLocation.framework */; };
1011
9112E8871E724C320022A1CB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9112E8861E724C320022A1CB /* main.m */; };
1112
9112E88A1E724C320022A1CB /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 9112E8891E724C320022A1CB /* AppDelegate.m */; };
1213
9112E88D1E724C320022A1CB /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9112E88C1E724C320022A1CB /* ViewController.m */; };
@@ -50,6 +51,7 @@
5051
/* End PBXCopyFilesBuildPhase section */
5152

5253
/* Begin PBXFileReference section */
54+
03432CDB1EBD426A0071FC48 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
5355
9112E8821E724C320022A1CB /* OneSignalDevApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OneSignalDevApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
5456
9112E8861E724C320022A1CB /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
5557
9112E8881E724C320022A1CB /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -78,6 +80,7 @@
7880
isa = PBXFrameworksBuildPhase;
7981
buildActionMask = 2147483647;
8082
files = (
83+
03432CDC1EBD426A0071FC48 /* CoreLocation.framework in Frameworks */,
8184
9112E8A71E724EE00022A1CB /* SystemConfiguration.framework in Frameworks */,
8285
9112E8A51E724ECC0022A1CB /* libOneSignal.a in Frameworks */,
8386
);
@@ -145,6 +148,7 @@
145148
9112E8A21E724DCA0022A1CB /* Frameworks */ = {
146149
isa = PBXGroup;
147150
children = (
151+
03432CDB1EBD426A0071FC48 /* CoreLocation.framework */,
148152
91B6EA051E83215000B5CF01 /* UserNotifications.framework */,
149153
9150E7821E73BFB600C5D46A /* UIKit.framework */,
150154
9150E77F1E73BF5B00C5D46A /* OneSignal.framework */,

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4949
[OneSignal initWithLaunchOptions:launchOptions
5050
appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba"
5151
handleNotificationAction:openNotificationHandler
52-
settings:@{kOSSettingsKeyAutoPrompt: @false}];
52+
settings:@{kOSSettingsKeyAutoPrompt: @false,
53+
kOSSettingsKeyInAppLaunchURL: @false}];
5354

55+
[OneSignal promptLocation];
5456
[OneSignal sendTag:@"someKey1122" value:@"03222017"];
5557

5658
[OneSignal addPermissionObserver:self];

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Info.plist

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>NSLocationWhenInUseUsageDescription</key>
6+
<string>Test Location2</string>
7+
<key>NSLocationUsageDescription</key>
8+
<string>Test Location</string>
59
<key>CFBundleDevelopmentRegion</key>
610
<string>en</string>
711
<key>CFBundleExecutable</key>
@@ -45,10 +49,10 @@
4549
<string>UIInterfaceOrientationLandscapeLeft</string>
4650
<string>UIInterfaceOrientationLandscapeRight</string>
4751
</array>
48-
<key>NSAppTransportSecurity</key>
49-
<dict>
50-
<key>NSAllowsArbitraryLoads</key>
51-
<true/>
52-
</dict>
52+
<key>NSAppTransportSecurity</key>
53+
<dict>
54+
<key>NSAllowsArbitraryLoads</key>
55+
<true/>
56+
</dict>
5357
</dict>
5458
</plist>

iOS_SDK/OneSignalSDK/Source/OneSignalLocation.m

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ @implementation OneSignalLocation
6161
#pragma clang diagnostic push
6262
#pragma clang diagnostic ignored "-Wundeclared-selector"
6363

64+
65+
NSObject *_mutexObjectForLastLocation;
66+
+(NSObject*)mutexObjectForLastLocation {
67+
if (!_mutexObjectForLastLocation)
68+
_mutexObjectForLastLocation = [NSObject alloc];
69+
return _mutexObjectForLastLocation;
70+
}
71+
6472
static OneSignalLocation* singleInstance = nil;
6573
+(OneSignalLocation*) sharedInstance {
6674
@synchronized( singleInstance ) {
@@ -76,7 +84,9 @@ + (os_last_location*)lastLocation {
7684
return lastLocation;
7785
}
7886
+ (void)clearLastLocation {
79-
lastLocation = nil;
87+
@synchronized(OneSignalLocation.mutexObjectForLastLocation) {
88+
lastLocation = nil;
89+
}
8090
}
8191

8292
+ (void) getLocation:(bool)prompt {
@@ -212,7 +222,9 @@ - (void)locationManager:(id)manager didUpdateLocations:(NSArray *)locations {
212222
currentLocation->horizontalAccuracy = [[location valueForKey:@"horizontalAccuracy"] doubleValue];
213223
currentLocation->cords = cords;
214224

215-
lastLocation = currentLocation;
225+
@synchronized(OneSignalLocation.mutexObjectForLastLocation) {
226+
lastLocation = currentLocation;
227+
}
216228

217229
if(!sendLocationTimer)
218230
[OneSignalLocation resetSendTimer];
@@ -227,11 +239,12 @@ + (void)resetSendTimer {
227239
sendLocationTimer = [NSTimer scheduledTimerWithTimeInterval:requiredWaitTime target:self selector:@selector(sendLocation) userInfo:nil repeats:NO];
228240
}
229241

230-
+ (void) sendLocation {
242+
+ (void)sendLocation {
243+
@synchronized(OneSignalLocation.mutexObjectForLastLocation) {
231244
if (!lastLocation || ![OneSignal mUserId]) return;
232245

233246
//Fired from timer and not initial location fetched
234-
if(initialLocationSent)
247+
if (initialLocationSent)
235248
[OneSignalLocation resetSendTimer];
236249

237250
initialLocationSent = YES;
@@ -256,6 +269,7 @@ + (void) sendLocation {
256269
[OneSignalHelper enqueueRequest:request
257270
onSuccess:nil
258271
onFailure:nil];
272+
}
259273
}
260274

261275

0 commit comments

Comments
 (0)