Skip to content

Commit

Permalink
Release 0.9.10 (#66)
Browse files Browse the repository at this point in the history
* (For Janice) Hunts down remaining instances of UI being queried on bg thread (#73)

* Hunts down remaining instances of UI being queried on bg thread

* Move some things out of async block

* Bum da version (#75)
  • Loading branch information
garylee1 authored Sep 7, 2017
1 parent 3305164 commit 41b9b30
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 86 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [0.9.10] - 2017-09-07
- Fixes iOS 11 crasher

## [0.9.9] - 2017-08-17
- Excludes NaN and Infinity values from JSON body
- Adds collector for is_simulator
Expand Down
2 changes: 1 addition & 1 deletion HelloSift/Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "SiftScience/sift-ios" ~> 0.9.9
github "SiftScience/sift-ios" ~> 0.9.10
2 changes: 1 addition & 1 deletion Sift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'Sift'
spec.version = '0.9.9'
spec.version = '0.9.10'
spec.authors = 'Sift Science'
spec.license = {
:type => 'MIT',
Expand Down
153 changes: 81 additions & 72 deletions Sift/SFIosAppStateCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,90 +176,99 @@ - (void)requestCollectionWithTitle:(NSString *)title {
}

- (void)checkAndCollectWhenNoneRecently:(SFTimestamp)now {
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
SF_DEBUG(@"Ignore collection request since the app is in the background");
return;
}

if (_lastCollectedAt > 0) {
if (_lastCollectedAt + SF_MAX_COLLECTION_PERIOD >= now) {
SF_DEBUG(@"Ignore collection request since it is not long enough since last collection");
dispatch_async(dispatch_get_main_queue(), ^{
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
SF_DEBUG(@"Ignore collection request since the app is in the background");
return;
}
}

[self collectWithTitle:nil andTimestamp:now];

dispatch_async(_serial, ^{
if (_lastCollectedAt > 0) {
if (_lastCollectedAt + SF_MAX_COLLECTION_PERIOD >= now) {
SF_DEBUG(@"Ignore collection request since it is not long enough since last collection");
return;
}
}

[self collectWithTitle:nil andTimestamp:now];
});
});
}

- (void)collectWithTitle:(NSString *)title andTimestamp:(SFTimestamp)now {
SF_DEBUG(@"Collect app state...");
SFEvent *event = [SFEvent new];
event.time = now;
event.iosAppState = SFCollectIosAppState(_locationManager, title);

BOOL foreground = UIApplication.sharedApplication.applicationState != UIApplicationStateBackground;

// Don't start compass and motion sensors when you are in the background.
int64_t delay = 0;
if (foreground) {
if (_allowUsingMotionSensors) {
SF_DEBUG(@"Collect motion data...");
[self startMotionSensors];
// Wait for a full cycle of readings plus 0.1 second margin to collect motion sensor readings.
delay = MAX(delay, (SF_MOTION_SENSOR_INTERVAL * SF_MOTION_SENSOR_NUM_READINGS + 0.1) * NSEC_PER_SEC);
}

[_locationManager startUpdatingHeading];
delay = MAX(delay, SF_HEADING_INTERVAL);
}
dispatch_async(dispatch_get_main_queue(), ^{
SF_DEBUG(@"Collect app state...");
SFEvent *event = [SFEvent new];
event.time = now;
event.iosAppState = SFCollectIosAppState(_locationManager, title);

if (delay > 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), _serial, ^{
[self stopMotionSensors];

if ([self canCollectLocationData] && _locationManager.location) {
[event.iosAppState setEntry:@"location" value:SFCLLocationToDictionary(_locationManager.location).entries];
BOOL foreground = UIApplication.sharedApplication.applicationState != UIApplicationStateBackground;

dispatch_async(_serial, ^{
// Don't start compass and motion sensors when you are in the background.
int64_t delay = 0;
if (foreground) {
if (_allowUsingMotionSensors) {
SF_DEBUG(@"Collect motion data...");
[self startMotionSensors];
// Wait for a full cycle of readings plus 0.1 second margin to collect motion sensor readings.
delay = MAX(delay, (SF_MOTION_SENSOR_INTERVAL * SF_MOTION_SENSOR_NUM_READINGS + 0.1) * NSEC_PER_SEC);
}

[_locationManager startUpdatingHeading];
delay = MAX(delay, SF_HEADING_INTERVAL);
}

// Read heading before we stop location manager (it nullifies heading when stopped).
CLHeading *heading = _locationManager.heading;
[_locationManager stopUpdatingHeading];

if (heading) {
[event.iosAppState setEntry:@"heading" value:SFCLHeadingToDictionary(heading).entries];
if (delay > 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), _serial, ^{
[self stopMotionSensors];

if ([self canCollectLocationData] && _locationManager.location) {
[event.iosAppState setEntry:@"location" value:SFCLLocationToDictionary(_locationManager.location).entries];
}

// Read heading before we stop location manager (it nullifies heading when stopped).
CLHeading *heading = _locationManager.heading;
[_locationManager stopUpdatingHeading];

if (heading) {
[event.iosAppState setEntry:@"heading" value:SFCLHeadingToDictionary(heading).entries];
}

[self addReadingsToIosAppState:event.iosAppState];

SF_DEBUG(@"iosAppState: %@", event.iosAppState.entries);
[Sift.sharedInstance appendEvent:event];
});
} else {
if ([self canCollectLocationData] && _locationManager.location) {
[event.iosAppState setEntry:@"location" value:SFCLLocationToDictionary(_locationManager.location).entries];
}

CLHeading *heading = _locationManager.heading;
if (heading) {
[event.iosAppState setEntry:@"heading" value:SFCLHeadingToDictionary(heading).entries];
}

[self addReadingsToIosAppState:event.iosAppState];

SF_DEBUG(@"iosAppState: %@", event.iosAppState.entries);
[Sift.sharedInstance appendEvent:event];
}

[self addReadingsToIosAppState:event.iosAppState];

SF_DEBUG(@"iosAppState: %@", event.iosAppState.entries);
[Sift.sharedInstance appendEvent:event];
});
} else {
if ([self canCollectLocationData] && _locationManager.location) {
[event.iosAppState setEntry:@"location" value:SFCLLocationToDictionary(_locationManager.location).entries];
}

CLHeading *heading = _locationManager.heading;
if (heading) {
[event.iosAppState setEntry:@"heading" value:SFCLHeadingToDictionary(heading).entries];
}

[self addReadingsToIosAppState:event.iosAppState];

SF_DEBUG(@"iosAppState: %@", event.iosAppState.entries);
[Sift.sharedInstance appendEvent:event];
}

_lastCollectedAt = now;
_lastCollectedAt = now;

// Don't schedule a check in the future if you are in the background.
if (foreground) {
// We don't care whether the remaining requests are executed if we are gone.
SFIosAppStateCollector * __weak weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, SF_MAX_COLLECTION_PERIOD * NSEC_PER_MSEC), _serial, ^{
[weakSelf checkAndCollectWhenNoneRecently:SFCurrentTime()];
});
}

// Don't schedule a check in the future if you are in the background.
if (foreground) {
// We don't care whether the remaining requests are executed if we are gone.
SFIosAppStateCollector * __weak weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, SF_MAX_COLLECTION_PERIOD * NSEC_PER_MSEC), _serial, ^{
[weakSelf checkAndCollectWhenNoneRecently:SFCurrentTime()];
});
}
});
}

#pragma mark - NSKeyedArchiver/NSKeyedUnarchiver
Expand Down
14 changes: 8 additions & 6 deletions Sift/SFQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ - (void)append:(SFEvent *)event {
// before terminating your app and thus we have to persist data
// aggressively when the app is in background. Hopefully there
// won't be too many events when app is in the background.
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
[self archive];
});
}

dispatch_async(dispatch_get_main_queue(), ^{
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
[self archive];
});
}
});

if (self.readyForUpload) {
[self requestUpload];
_lastUploadTimestamp = SFCurrentTime();
Expand Down
17 changes: 12 additions & 5 deletions Sift/SFUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ - (void)upload:(NSArray *)events {
dispatch_async(_serial, ^{
SF_DEBUG(@"Batch size: %lu", (unsigned long)events.count);
[_batches addObject:events];
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
// Back up aggressively if we are in the background.
[self archive];
}
[self doUpload];

dispatch_async(dispatch_get_main_queue(), ^{
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
// Back up aggressively if we are in the background.
dispatch_async(_serial, ^{
[self archive];
});
}
dispatch_async(_serial, ^{
[self doUpload];
});
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion Sift/Sift.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ + (instancetype)sharedInstance {
- (instancetype)initWithRootDirPath:(NSString *)rootDirPath {
self = [super init];
if (self) {
_sdkVersion = @"v0.9.9";
_sdkVersion = @"v0.9.10";

_rootDirPath = rootDirPath;

Expand Down

0 comments on commit 41b9b30

Please sign in to comment.