Skip to content

Commit

Permalink
Fix sending logs from applicationWillTerminate
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Kocheshkova committed Aug 17, 2020
1 parent f7b7a46 commit 09e85a7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
31 changes: 31 additions & 0 deletions AppCenter/AppCenter/Internals/Channel/MSChannelGroupDefault.m
Expand Up @@ -13,6 +13,12 @@

@implementation MSChannelGroupDefault

#if !TARGET_OS_OSX

@synthesize delayedProcessingSemaphore = _delayedProcessingSemaphore;

#endif

#pragma mark - Initialization

- (instancetype)initWithHttpClient:(id<MSHttpClientProtocol>)httpClient installId:(NSUUID *)installId logUrl:(NSString *)logUrl {
Expand All @@ -32,6 +38,9 @@ - (instancetype)initWithIngestion:(nullable MSAppCenterIngestion *)ingestion {
if (ingestion) {
_ingestion = ingestion;
}
#if !TARGET_OS_OSX
_delayedProcessingSemaphore = dispatch_semaphore_create(0);
#endif
}
return self;
}
Expand Down Expand Up @@ -177,6 +186,17 @@ - (void)channel:(id<MSChannelProtocol>)channel didResumeWithIdentifyingObject:(i

- (void)setEnabled:(BOOL)isEnabled andDeleteDataOnDisabled:(BOOL)deleteData {

#if !TARGET_OS_OSX
if (isEnabled) {
[MS_NOTIFICATION_CENTER addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
} else {
[MS_NOTIFICATION_CENTER removeObserver:self];
}
#endif

// Propagate to ingestion.
[self.ingestion setEnabled:isEnabled andDeleteDataOnDisabled:deleteData];

Expand All @@ -199,6 +219,17 @@ - (void)setEnabled:(BOOL)isEnabled andDeleteDataOnDisabled:(BOOL)deleteData {
*/
}

#if !TARGET_OS_OSX
- (void)applicationWillTerminate:(__unused UIApplication *)application {

// Block logs queue so that it isn't killed before app termination.
dispatch_async(self.logsDispatchQueue, ^{
dispatch_semaphore_signal(self.delayedProcessingSemaphore);
});
dispatch_semaphore_wait(self.delayedProcessingSemaphore, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC));
}
#endif

#pragma mark - Pause / Resume

- (void)pauseWithIdentifyingObject:(id<NSObject>)identifyingObject {
Expand Down
Expand Up @@ -18,6 +18,19 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (instancetype)initWithIngestion:(nullable MSAppCenterIngestion *)ingestion;

#if !TARGET_OS_OSX
/**
* Semaphore for blocking logs queue on applicationWillTerminate.
*/
@property dispatch_semaphore_t delayedProcessingSemaphore;

/**
* Called when applciation is terminating.
*/
- (void)applicationWillTerminate:(__unused UIApplication *)application;

#endif

@end

NS_ASSUME_NONNULL_END
33 changes: 33 additions & 0 deletions AppCenter/AppCenterTests/MSChannelGroupDefaultTests.m
@@ -1,6 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#import "TargetConditionals.h"
#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#endif

#import "MSAbstractLogInternal.h"
#import "MSAppCenterIngestion.h"
#import "MSChannelDelegate.h"
Expand Down Expand Up @@ -61,6 +66,34 @@ - (void)tearDown {
[super tearDown];
}

#if !TARGET_OS_OSX
- (void)testAppIsKilled {

// If
[self.sut setEnabled:YES andDeleteDataOnDisabled:YES];
id sut = OCMPartialMock(self.sut);

// When
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification object:sut];

// Then
OCMVerify([sut applicationWillTerminate:OCMOCK_ANY]);
XCTAssertNotNil(self.sut.logsDispatchQueue);

// If
[self.sut setEnabled:NO andDeleteDataOnDisabled:YES];
OCMReject([sut applicationWillTerminate:OCMOCK_ANY]);

// When
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification object:sut];

// Then
self.sut.logsDispatchQueue = nil;
OCMVerifyAll(sut);
[sut stopMocking];
}
#endif

#pragma mark - Tests

- (void)testNewInstanceWasInitialisedCorrectly {
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

* **[Fix]** Fix compatibility with Xcode 12 beta when integrating SDK from sources.

### App Center Analytics

* **[Fix]** Fix sending logs from `applicationWillTerminate`.

___

## Version 3.3.2
Expand Down

0 comments on commit 09e85a7

Please sign in to comment.