Skip to content

Commit

Permalink
Add callback to channel unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Kocheshkova committed Aug 12, 2020
1 parent d05e398 commit a038984
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 58 deletions.
5 changes: 0 additions & 5 deletions AppCenter/AppCenter/Internals/Channel/MSChannelGroupDefault.h
Expand Up @@ -32,11 +32,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (instancetype)initWithHttpClient:(id<MSHttpClientProtocol>)httpClient installId:(NSUUID *)installId logUrl:(NSString *)logUrl;

/**
* Flag that indicates whether the app has hit applicationWillTerminate state.
*/
+ (BOOL)hasEnteredApplicationWillTerminate;

/**
* Collection of channel delegates.
*/
Expand Down
39 changes: 0 additions & 39 deletions AppCenter/AppCenter/Internals/Channel/MSChannelGroupDefault.m
Expand Up @@ -15,14 +15,6 @@

@implementation MSChannelGroupDefault

static BOOL _applicationWillTerminate = NO;

#if !TARGET_OS_OSX

@synthesize delayedProcessingSemaphore = _delayedProcessingSemaphore;

#endif

#pragma mark - Initialization

- (instancetype)initWithHttpClient:(id<MSHttpClientProtocol>)httpClient installId:(NSUUID *)installId logUrl:(NSString *)logUrl {
Expand All @@ -34,7 +26,6 @@ - (instancetype)initWithHttpClient:(id<MSHttpClientProtocol>)httpClient installI

- (instancetype)initWithIngestion:(nullable MSAppCenterIngestion *)ingestion {
if ((self = [self init])) {
_applicationWillTerminate = NO;
dispatch_queue_t serialQueue = dispatch_queue_create(kMSLogsDispatchQueue, DISPATCH_QUEUE_SERIAL);
_logsDispatchQueue = serialQueue;
_channels = [NSMutableArray<id<MSChannelUnitProtocol>> new];
Expand All @@ -43,17 +34,10 @@ - (instancetype)initWithIngestion:(nullable MSAppCenterIngestion *)ingestion {
if (ingestion) {
_ingestion = ingestion;
}
#if !TARGET_OS_OSX
_delayedProcessingSemaphore = dispatch_semaphore_create(0);
#endif
}
return self;
}

+ (BOOL)hasEnteredApplicationWillTerminate {
return _applicationWillTerminate;
}

- (id<MSChannelUnitProtocol>)addChannelUnitWithConfiguration:(MSChannelUnitConfiguration *)configuration {
return [self addChannelUnitWithConfiguration:configuration withIngestion:self.ingestion];
}
Expand Down Expand Up @@ -195,17 +179,6 @@ - (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 @@ -228,18 +201,6 @@ - (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, ^{
_applicationWillTerminate = YES;
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,19 +18,6 @@ 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
35 changes: 34 additions & 1 deletion AppCenter/AppCenter/Internals/Channel/MSChannelUnitDefault.m
Expand Up @@ -20,13 +20,21 @@

@implementation MSChannelUnitDefault

#if !TARGET_OS_OSX

@synthesize delayedProcessingSemaphore = _delayedProcessingSemaphore;
@synthesize applicationWillTerminateEntered = _applicationWillTerminateEntered;

#endif

@synthesize configuration = _configuration;
@synthesize logsDispatchQueue = _logsDispatchQueue;

#pragma mark - Initialization

- (instancetype)init {
if ((self = [super init])) {
_applicationWillTerminateEntered = NO;
_itemsCount = 0;
_pendingBatchIds = [NSMutableArray new];
_pendingBatchQueueFull = NO;
Expand All @@ -37,6 +45,9 @@ - (instancetype)init {
_delegates = [NSHashTable weakObjectsHashTable];
_pausedIdentifyingObjects = [NSHashTable weakObjectsHashTable];
_pausedTargetKeys = [NSMutableSet new];
#if !TARGET_OS_OSX
_delayedProcessingSemaphore = dispatch_semaphore_create(0);
#endif
}
return self;
}
Expand Down Expand Up @@ -168,7 +179,7 @@ - (void)enqueueItem:(id<MSLog>)item flags:(MSFlags)flags {
}
};

if ([MSChannelGroupDefault hasEnteredApplicationWillTerminate]) {
if (self.applicationWillTerminateEntered) {

// Process synchronously in case applicationWillTerminate was hit.
dispatch_sync(self.logsDispatchQueue, storeLogs);
Expand All @@ -179,6 +190,18 @@ - (void)enqueueItem:(id<MSLog>)item flags:(MSFlags)flags {
}
}

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

self.applicationWillTerminateEntered = YES;
// 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

- (void)sendLogContainer:(MSLogContainer *__nonnull)container {

// Add to pending batches.
Expand Down Expand Up @@ -428,6 +451,16 @@ - (void)resetTimer {
#pragma mark - Life cycle

- (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
dispatch_async(self.logsDispatchQueue, ^{
if (self.enabled != isEnabled) {
self.enabled = isEnabled;
Expand Down
Expand Up @@ -11,6 +11,21 @@ NS_ASSUME_NONNULL_BEGIN

@property(nonatomic) NSMutableSet<NSString *> *pausedTargetKeys;

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

@property BOOL applicationWillTerminateEntered;

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

#endif

/**
* Flush pending logs.
*/
Expand Down

0 comments on commit a038984

Please sign in to comment.