Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #489 from bitstadium/master
Browse files Browse the repository at this point in the history
Master -> develop
  • Loading branch information
Benjamin Scholtysik (Reimold) authored Dec 12, 2017
2 parents c014376 + d4de7b2 commit c97cb5e
Show file tree
Hide file tree
Showing 56 changed files with 1,284 additions and 223 deletions.
12 changes: 7 additions & 5 deletions Classes/BITAuthenticator.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "BITAuthenticationViewController.h"
#import "BITHockeyAppClient.h"
#import "BITHockeyHelper.h"
#import "BITHockeyHelper+Application.h"
#import "BITHockeyBaseManagerPrivate.h"

#include <sys/stat.h>
Expand Down Expand Up @@ -105,12 +106,13 @@ - (void)authenticateInstallation {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(authenticateInstallation) object:nil];
[self performSelector:@selector(authenticateInstallation) withObject:nil afterDelay:0.1];
} else {
switch ([[UIApplication sharedApplication] applicationState]) {
case UIApplicationStateActive:
switch ([BITHockeyHelper applicationState]) {
case BITApplicationStateActive:
[self authenticate];
break;
case UIApplicationStateBackground:
case UIApplicationStateInactive:
case BITApplicationStateBackground:
case BITApplicationStateInactive:
case BITApplicationStateUnknown:
// do nothing, wait for active state
break;
}
Expand Down Expand Up @@ -743,7 +745,7 @@ + (void)email:(NSString *__autoreleasing *)email andIUID:(NSString *__autoreleas
#pragma mark - Private helpers

- (void)alertOnFailureStoringTokenInKeychain {
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
if ([BITHockeyHelper applicationState] != BITApplicationStateActive) {
return;
}

Expand Down
107 changes: 61 additions & 46 deletions Classes/BITChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import "BITHockeyManager.h"
#import "BITChannelPrivate.h"
#import "BITHockeyHelper.h"
#import "BITHockeyHelper+Application.h"
#import "BITTelemetryContext.h"
#import "BITTelemetryData.h"
#import "BITEnvelope.h"
Expand Down Expand Up @@ -33,10 +34,6 @@
@interface BITChannel ()

@property (nonatomic, weak, nullable) id appDidEnterBackgroundObserver;
@property (nonatomic, weak, nullable) id persistenceSuccessObserver;
@property (nonatomic, weak, nullable) id senderFinishSendingDataObserver;

@property (nonatomic, nonnull) dispatch_group_t senderGroup;

@end

Expand All @@ -61,8 +58,6 @@ - (instancetype)init {
dispatch_queue_t serialQueue = dispatch_queue_create(BITDataItemsOperationsQueue, DISPATCH_QUEUE_SERIAL);
_dataItemsOperations = serialQueue;

_senderGroup = dispatch_group_create();

[self registerObservers];
}
return self;
Expand Down Expand Up @@ -107,26 +102,6 @@ - (void) registerObservers {
queue:NSOperationQueue.mainQueue
usingBlock:notificationBlock];
}
if (nil == self.persistenceSuccessObserver) {
self.persistenceSuccessObserver =
[center addObserverForName:BITPersistenceSuccessNotification
object:nil
queue:nil
usingBlock:^(NSNotification __unused *notification) {
typeof(self) strongSelf = weakSelf;
dispatch_group_enter(strongSelf.senderGroup);
}];
}
if (nil == self.senderFinishSendingDataObserver) {
self.senderFinishSendingDataObserver =
[center addObserverForName:BITSenderFinishSendingDataNotification
object:nil
queue:nil
usingBlock:^(NSNotification __unused *notification) {
typeof(self) strongSelf = weakSelf;
dispatch_group_leave(strongSelf.senderGroup);
}];
}
}

- (void) unregisterObservers {
Expand All @@ -136,16 +111,6 @@ - (void) unregisterObservers {
[center removeObserver:appDidEnterBackgroundObserver];
self.appDidEnterBackgroundObserver = nil;
}
id persistenceSuccessObserver = self.persistenceSuccessObserver;
if (persistenceSuccessObserver) {
[center removeObserver:persistenceSuccessObserver];
self.persistenceSuccessObserver = nil;
}
id senderFinishSendingDataObserver = self.senderFinishSendingDataObserver;
if (senderFinishSendingDataObserver) {
[center removeObserver:senderFinishSendingDataObserver];
self.senderFinishSendingDataObserver = nil;
}
}

#pragma mark - Queue management
Expand Down Expand Up @@ -201,37 +166,71 @@ - (void)persistDataItemQueueWithBackgroundTask:(UIApplication *)application {
typeof(self) strongSelf = weakSelf;
[strongSelf persistDataItemQueue:&BITTelemetryEventBuffer];
});
[self createBackgroundTask:application withWaitingGroup:nil];
[self createBackgroundTaskWhileDataIsSending:application withWaitingGroup:nil];
}

- (void)createBackgroundTask:(UIApplication *)application withWaitingGroup:(nullable dispatch_group_t)group {
- (void)createBackgroundTaskWhileDataIsSending:(UIApplication *)application
withWaitingGroup:(nullable dispatch_group_t)group {
if (application == nil) {
return;
}

// Queues needs for waiting consistently.
NSArray *queues = @[
self.dataItemsOperations, // For enqueue
self.persistence.persistenceQueue, // For persist
dispatch_get_main_queue() // For notification
];

// Tracking for sender activity.
// BITPersistenceSuccessNotification - start sending
// BITSenderFinishSendingDataNotification - finish sending
__block dispatch_group_t senderGroup = dispatch_group_create();
__block NSInteger senderCounter = 0;
__block id persistenceSuccessObserver = [[NSNotificationCenter defaultCenter]
addObserverForName:BITPersistenceSuccessNotification
object:nil
queue:nil
usingBlock:^(__unused NSNotification *notification) {
dispatch_group_enter(senderGroup);
senderCounter++;
if (persistenceSuccessObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:persistenceSuccessObserver];
persistenceSuccessObserver = nil;
}
}];
__block id senderFinishSendingDataObserver = [[NSNotificationCenter defaultCenter]
addObserverForName:BITSenderFinishSendingDataNotification
object:nil
queue:nil
usingBlock:^(__unused NSNotification *notification) {
if (senderCounter > 0) {
dispatch_group_leave(senderGroup);
senderCounter--;
}
if (senderFinishSendingDataObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:senderFinishSendingDataObserver];
senderFinishSendingDataObserver = nil;
}
}];

BITHockeyLogVerbose(@"BITChannel: Start background task");
__block UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
BITHockeyLogVerbose(@"BITChannel: Background task is expired");
[application endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
__block NSUInteger i = 0;
__weak typeof(self) weakSelf = self;
__block __weak void (^weakWaitBlock)();
void (^waitBlock)();
__block __weak void (^weakWaitBlock)(void);
void (^waitBlock)(void);
weakWaitBlock = waitBlock = ^{
typeof(self) strongSelf = weakSelf;
if (i < queues.count) {
dispatch_queue_t queue = [queues objectAtIndex:i++];
BITHockeyLogVerbose(@"BITChannel: Waiting queue: %@", [[NSString alloc] initWithUTF8String:dispatch_queue_get_label(queue)]);
dispatch_async(queue, weakWaitBlock);
} else {
BITHockeyLogVerbose(@"BITChannel: Waiting sender");
dispatch_group_notify(strongSelf.senderGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_group_notify(senderGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (backgroundTask != UIBackgroundTaskInvalid) {
BITHockeyLogVerbose(@"BITChannel: Cancel background task");
[application endBackgroundTask:backgroundTask];
Expand Down Expand Up @@ -259,11 +258,17 @@ - (void)resetQueue {
#pragma mark - Adding to queue

- (void)enqueueTelemetryItem:(BITTelemetryData *)item {

[self enqueueTelemetryItem:item completionHandler:nil];
}

- (void)enqueueTelemetryItem:(BITTelemetryData *)item completionHandler:(nullable void (^)(void))completionHandler {
if (!item) {

// Item is nil: Do not enqueue item and abort operation.
BITHockeyLogWarning(@"WARNING: TelemetryItem was nil.");
if(completionHandler) {
completionHandler();
}
return;
}

Expand All @@ -280,16 +285,22 @@ - (void)enqueueTelemetryItem:(BITTelemetryData *)item {
if (![strongSelf timerIsRunning]) {
[strongSelf startTimer];
}

if(completionHandler) {
completionHandler();
}

return;
}

// Enqueue item.
@synchronized(self) {
NSDictionary *dict = [strongSelf dictionaryForTelemetryData:item];
[strongSelf appendDictionaryToEventBuffer:dict];
UIApplication *application = [UIApplication sharedApplication];
// If the app is running in the background.
BOOL applicationIsInBackground = ([BITHockeyHelper applicationState] == BITApplicationStateBackground);
if (strongSelf.dataItemCount >= strongSelf.maxBatchSize ||
(application && application.applicationState == UIApplicationStateBackground)) {
(applicationIsInBackground)) {

// Case 2: Max batch count has been reached or the app is running in the background, so write queue to disk and delete all items.
[strongSelf persistDataItemQueue:&BITTelemetryEventBuffer];
Expand All @@ -300,6 +311,10 @@ - (void)enqueueTelemetryItem:(BITTelemetryData *)item {
[strongSelf startTimer];
}
}

if(completionHandler) {
completionHandler();
}
}
});
}
Expand Down
12 changes: 11 additions & 1 deletion Classes/BITChannelPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ FOUNDATION_EXPORT NSString *const BITChannelBlockedNotification;
/**
* Create background task for queues and group.
*/
- (void)createBackgroundTask:(UIApplication *)application withWaitingGroup:(nullable dispatch_group_t)group;
- (void)createBackgroundTaskWhileDataIsSending:(UIApplication *)application
withWaitingGroup:(nullable dispatch_group_t)group;

/**
* Adds the specified dictionary to the JSON Stream string.
Expand Down Expand Up @@ -108,6 +109,15 @@ void bit_resetEventBuffer(char *__nonnull*__nonnull eventBuffer);
*/
- (BOOL)isQueueBusy;

/**
* Enqueue a telemetry item. This is for testing purposes where we actually use the completion handler.
*
* @param completionHandler The completion handler that will be called after enqueuing a BITTelemetryData object.
*
* @discussion intended for testing purposes.
*/
- (void)enqueueTelemetryItem:(BITTelemetryData *)item completionHandler:(nullable void (^)(void))completionHandler;

@end

NS_ASSUME_NONNULL_END
Expand Down
6 changes: 3 additions & 3 deletions Classes/BITCrashManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#import "HockeySDKPrivate.h"
#import "BITHockeyHelper.h"
#import "BITHockeyHelper+Application.h"
#import "BITHockeyAppClient.h"

#import "BITCrashManager.h"
Expand Down Expand Up @@ -1052,8 +1053,7 @@ - (void)triggerDelayedProcessing {
*/
- (void)invokeDelayedProcessing {
#if !defined (HOCKEYSDK_CONFIGURATION_ReleaseCrashOnlyExtensions)
if (!bit_isRunningInAppExtension() &&
[[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
if ([BITHockeyHelper applicationState] != BITApplicationStateActive) {
return;
}
#endif
Expand Down Expand Up @@ -1277,7 +1277,7 @@ - (void)startManager {
}

#if !defined (HOCKEYSDK_CONFIGURATION_ReleaseCrashOnlyExtensions)
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
if ([BITHockeyHelper applicationState] != BITApplicationStateActive) {
[self appEnteredForeground];
}
#else
Expand Down
8 changes: 4 additions & 4 deletions Classes/BITFeedbackListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
#pragma mark - ListViewCellDelegate

- (void)listCell:(id) __unused cell didSelectAttachment:(BITFeedbackMessageAttachment *)attachment {
if (!self.cachedPreviewItems){
[self refreshPreviewItems];
}

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;

Expand All @@ -739,10 +743,6 @@ - (void)refreshPreviewItems {
}

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *) __unused controller {
if (!self.cachedPreviewItems){
[self refreshPreviewItems];
}

return self.cachedPreviewItems.count;
}

Expand Down
12 changes: 7 additions & 5 deletions Classes/BITFeedbackManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#import "HockeySDKNullability.h"
#import "BITHockeyHelper.h"
#import "BITHockeyHelper+Application.h"
#import "BITHockeyAppClient.h"

#define kBITFeedbackUserDataAsked @"HockeyFeedbackUserDataAsked"
Expand Down Expand Up @@ -131,7 +132,7 @@ - (void)didBecomeActiveActions {
- (void)didEnterBackgroundActions {
self.didEnterBackgroundState = NO;

if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
if ([BITHockeyHelper applicationState] == BITApplicationStateBackground) {
self.didEnterBackgroundState = YES;
}
}
Expand Down Expand Up @@ -270,15 +271,16 @@ - (void)startManager {
[self isiOS10PhotoPolicySet];

// we are already delayed, so the notification already came in and this won't invoked twice
switch ([[UIApplication sharedApplication] applicationState]) {
case UIApplicationStateActive:
switch ([BITHockeyHelper applicationState]) {
case BITApplicationStateActive:
// we did startup, so yes we are coming from background
self.didEnterBackgroundState = YES;

[self didBecomeActiveActions];
break;
case UIApplicationStateBackground:
case UIApplicationStateInactive:
case BITApplicationStateBackground:
case BITApplicationStateInactive:
case BITApplicationStateUnknown:
// do nothing, wait for active state
break;
}
Expand Down
Loading

0 comments on commit c97cb5e

Please sign in to comment.