...
|
...
|
@@ -7,7 +7,6 @@ |
|
|
//
|
|
|
|
|
|
#import "YH_EventCacheManager.h"
|
|
|
#import "YH_EventCacheFile.h"
|
|
|
#import <UIKit/UIKit.h>
|
|
|
#import "YH_EventDataSession.h"
|
|
|
#import "YH_EventDataFactory.h"
|
...
|
...
|
@@ -17,25 +16,27 @@ |
|
|
#import "YH_AnalyticEventData.h"
|
|
|
#import "YHLog.h"
|
|
|
#import "YH_EventDataFactory.h"
|
|
|
#import <YH_FoundationKit/YH_FoundationKit.h>
|
|
|
|
|
|
static NSString *const kParamKeyDevice = @"device";
|
|
|
static NSString *const kParamKeyList = @"events";
|
|
|
static NSString *const kParamKeyType = @"type";
|
|
|
|
|
|
@interface YH_EventCacheManager()
|
|
|
|
|
|
//@property (strong, nonatomic) NSMutableArray *eventCacheArray;
|
|
|
@property (strong, nonatomic) NSMutableArray *performanceCacheArray;
|
|
|
@property (strong, nonatomic) NSMutableArray *infoCacheArray;//cache custom & performance,click info send in realtime
|
|
|
@property (strong, nonatomic) YH_EventDataSession *eventDataSession;
|
|
|
@property (nonatomic, assign) YHEventReportStrategy mode;
|
|
|
@property (nonatomic,strong)YH_PerformanceAppInfo *performanceAppInfo;
|
|
|
@property (nonatomic,strong)NSTimer *timer;
|
|
|
@property (nonatomic,assign)long timeInterval;
|
|
|
@property (nonatomic, assign) YHEventReportStrategy mode;
|
|
|
@property (nonatomic,strong) YH_PerformanceAppInfo *appInfo;
|
|
|
@property (nonatomic,strong) dispatch_source_t timer;
|
|
|
@property (nonatomic,strong) dispatch_queue_t queue;
|
|
|
@property (nonatomic,assign) NSUInteger timeInterval;
|
|
|
@property (nonatomic, strong) YH_Reachability *intnetReachability;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation YH_EventCacheManager
|
|
|
|
|
|
static NSString *kParamKeyDevice = @"device";
|
|
|
static NSString *kParamKeyList = @"events";
|
|
|
static NSString *kParamKeyType = @"type";
|
|
|
|
|
|
+ (YH_EventCacheManager *)sharedInstance
|
|
|
{
|
|
|
static YH_EventCacheManager *sharedInstance = nil;
|
...
|
...
|
@@ -50,67 +51,33 @@ static NSString *kParamKeyType = @"type"; |
|
|
{
|
|
|
self = [super init];
|
|
|
if (self) {
|
|
|
self.timeInterval = 30;
|
|
|
self.mode = YHEventReportStrategyInterval;
|
|
|
self.performanceAppInfo = [[YH_PerformanceAppInfo alloc] init];
|
|
|
self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode: NSRunLoopCommonModes];
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
selector:@selector(handleMemoryWarning)
|
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
|
object:nil];
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
selector:@selector(handleApplicationBackgrounding)
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
object:nil];
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
selector:@selector(handleDidFinishLaunch)
|
|
|
name:UIApplicationDidFinishLaunchingNotification
|
|
|
object:nil];
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
selector:@selector(handleTerminate)
|
|
|
name:UIApplicationWillTerminateNotification
|
|
|
object:nil];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPushStatus) name:UIApplicationWillEnterForegroundNotification object:nil];
|
|
|
|
|
|
[self refreshPushStatus];
|
|
|
[[YH_Reachability reachabilityForInternetConnection] startNotifier];
|
|
|
_timeInterval = 30;
|
|
|
_mode = YHEventReportStrategyInterval;
|
|
|
_appInfo = [[YH_PerformanceAppInfo alloc] init];
|
|
|
_eventDataSession = [[YH_EventDataSession alloc] init];
|
|
|
_infoCacheArray = [NSMutableArray array];
|
|
|
_queue = yh_gcdSerialQueue(@"cn.yoho.buy.eventReport.serial");
|
|
|
_intnetReachability = [YH_Reachability reachabilityForInternetConnection];
|
|
|
[_intnetReachability startNotifier];
|
|
|
[self startTimer];
|
|
|
[self yh_observeNotificationName:UIApplicationDidReceiveMemoryWarningNotification action:@selector(handleMemoryWarning)];
|
|
|
[self yh_observeNotificationName:UIApplicationDidEnterBackgroundNotification action:@selector(handleApplicationBackgrounding)];
|
|
|
[self yh_observeNotificationName:UIApplicationWillEnterForegroundNotification action:@selector(handleApplicationForerounding)];
|
|
|
[self yh_observeNotificationName:UIApplicationDidFinishLaunchingNotification action:@selector(handleDidFinishLaunch)];
|
|
|
[self yh_observeNotificationName:UIApplicationWillTerminateNotification action:@selector(handleTerminate)];
|
|
|
[self yh_observeNotificationName:UIApplicationWillEnterForegroundNotification action:@selector(refreshAppInfo)];
|
|
|
[self yh_observeNotificationName:kYHReachabilityChangedNotification action:@selector(networkStateChange:)];
|
|
|
}
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
- (void)dealloc
|
|
|
{
|
|
|
[self.timer invalidate];
|
|
|
[[YH_Reachability reachabilityForInternetConnection] stopNotifier];
|
|
|
[self cancelTimer];
|
|
|
[_intnetReachability stopNotifier];
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
}
|
|
|
|
|
|
#pragma mark - Getters
|
|
|
|
|
|
- (NSMutableArray*)performanceCacheArray
|
|
|
{
|
|
|
if (_performanceCacheArray == nil) {
|
|
|
_performanceCacheArray = [[NSMutableArray alloc]initWithCapacity:0];
|
|
|
}
|
|
|
return _performanceCacheArray;
|
|
|
}
|
|
|
|
|
|
- (YH_EventDataSession*)eventDataSession
|
|
|
{
|
|
|
if (_eventDataSession == nil) {
|
|
|
_eventDataSession = [[YH_EventDataSession alloc]init];
|
|
|
}
|
|
|
return _eventDataSession;
|
|
|
}
|
|
|
|
|
|
#pragma mark - public
|
|
|
|
|
|
- (void)pushUIControlEvent:(NSString *)viewPath title:(NSString *)title content:(NSString *)content yh_pageName:(NSString *)yh_pageName yh_typeId:(NSString *)yh_typeId alias: (NSString *)yh_Alias
|
...
|
...
|
@@ -119,7 +86,6 @@ static NSString *kParamKeyType = @"type"; |
|
|
YHLog(@"UIControlEvent: viewPath can't empty!!!");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
NSDictionary *param = @{
|
|
|
kYHEventReportClickViewPath: viewPath,
|
|
|
kYHEventReportClickTitle: title ?: @"",
|
...
|
...
|
@@ -128,16 +94,15 @@ static NSString *kParamKeyType = @"type"; |
|
|
kYHEventReportClickVCPageName: yh_pageName?:@"",
|
|
|
kYHEventReportClickVCAlias: yh_Alias?:@"",
|
|
|
};
|
|
|
|
|
|
YHLog(@"UIControlEvent: %@", param);
|
|
|
|
|
|
[self pushEventData:param];
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)pushEventData:(NSDictionary*)data
|
|
|
{
|
|
|
[self pushData:[[YH_AnalyticEventData sharedInstance] analyticEventParameters:data] dataType:YHEventReportDataTypeClickEvent];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self pushData:[[YH_AnalyticEventData sharedInstance] analyticEventParameters:data] dataType:YHEventReportDataTypeClickEvent];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)pushPerformanceData:(NSDictionary*)data pointName:(YHEventReportPointName)name
|
...
|
...
|
@@ -146,19 +111,18 @@ static NSString *kParamKeyType = @"type"; |
|
|
[performanceDic setObject:@"PERFORMANCE" forKey:YOHOAppReportKeyPT];
|
|
|
[performanceDic setObject:[YH_EventDataFactory getPointName:name] forKey:YOHOAppReportKeyPN];
|
|
|
|
|
|
NSMutableDictionary *eventDic = [[YH_EventDataFactory factoryEventAppInfo:self.performanceAppInfo]mutableCopy];
|
|
|
NSMutableDictionary *eventDic = [NSMutableDictionary dictionary];
|
|
|
|
|
|
if (!IsDictionaryClass(eventDic)) {
|
|
|
return;
|
|
|
}
|
|
|
if (IsDictionaryClass(performanceDic)) {
|
|
|
[eventDic addEntriesFromDictionary:performanceDic];
|
|
|
}
|
|
|
if (IsDictionaryClass(eventDic)) {
|
|
|
if (IsDictionaryClass(data)) {
|
|
|
[eventDic setObject:data forKey:YOHOAppReportKeyPARAM];
|
|
|
}
|
|
|
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypePerformance];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[eventDic addEntriesFromDictionary:[YH_EventDataFactory factoryEventAppInfo:self.appInfo]];
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypePerformance];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)pushCustomData:(NSDictionary*)data pointType:(YHPointType)type pointName:(YHEventReportPointName)name{
|
...
|
...
|
@@ -166,103 +130,96 @@ static NSString *kParamKeyType = @"type"; |
|
|
[performanceDic setObject:[YH_EventDataFactory getPointType:type] forKey:YOHOAppReportKeyPT];
|
|
|
[performanceDic setObject:[YH_EventDataFactory getPointName:name] forKey:YOHOAppReportKeyPN];
|
|
|
|
|
|
NSMutableDictionary *eventDic = [[YH_EventDataFactory factoryEventAppInfo:self.performanceAppInfo]mutableCopy];
|
|
|
|
|
|
if (!IsDictionaryClass(eventDic)) {
|
|
|
return;
|
|
|
}
|
|
|
if (IsDictionaryClass(performanceDic)) {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
NSMutableDictionary *eventDic = [[YH_EventDataFactory factoryEventAppInfo:self.appInfo] mutableCopy];
|
|
|
[eventDic addEntriesFromDictionary:performanceDic];
|
|
|
}
|
|
|
|
|
|
switch (type) {
|
|
|
case YHPT_NETWORK:
|
|
|
|
|
|
switch (type) {
|
|
|
case YHPT_NETWORK:
|
|
|
{
|
|
|
[self.eventDataSession getbaiduNetStatus:eventDic completionBlock:^(NSTimeInterval cts, NSInteger errCode) {
|
|
|
NSMutableDictionary *appendDict = [[NSMutableDictionary alloc] initWithDictionary:data];
|
|
|
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%zd",errCode] forKey:YOHOAppReportKeyJsonKeyErrorTEC];
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%ld",(long)cts] forKey:YOHOAppReportKeyJsonKeyErrorCTS];
|
|
|
[eventDic setValue:appendDict forKey:YOHOAppReportKeyPARAM];
|
|
|
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypeCustom];
|
|
|
}];
|
|
|
[self checkBaiduStatusWithEventData:eventDic customData:data];
|
|
|
}
|
|
|
break;
|
|
|
case YHPT_AD:
|
|
|
break;
|
|
|
case YHPT_AD:
|
|
|
{
|
|
|
if ([data valueForKey:YOHOAppReportKeyJsonKeyErrorTEC]) {
|
|
|
[self.eventDataSession getbaiduNetStatus:eventDic completionBlock:^(NSTimeInterval cts, NSInteger errCode) {
|
|
|
NSMutableDictionary *appendDict = [[NSMutableDictionary alloc] initWithDictionary:data];
|
|
|
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%zd",errCode] forKey:YOHOAppReportKeyJsonKeyErrorTEC];
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%ld",(long)cts] forKey:YOHOAppReportKeyJsonKeyErrorCTS];
|
|
|
[eventDic setValue:appendDict forKey:YOHOAppReportKeyPARAM];
|
|
|
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypeCustom];
|
|
|
}];
|
|
|
[self checkBaiduStatusWithEventData:eventDic customData:data];
|
|
|
} else {
|
|
|
[eventDic setValue:data forKey:YOHOAppReportKeyPARAM];
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypeCustom];
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
default:
|
|
|
{
|
|
|
[eventDic setValue:data forKey:YOHOAppReportKeyPARAM];
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypeCustom];
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)pushCrashData:(NSDictionary*)data{
|
|
|
NSMutableDictionary *eventDic = [[YH_EventDataFactory factoryEventAppInfo:self.performanceAppInfo]mutableCopy];
|
|
|
|
|
|
if (!IsDictionaryClass(eventDic) ||!IsDictionaryClass(data)) {
|
|
|
- (void)checkBaiduStatusWithEventData:(NSMutableDictionary *)eventDic customData:(NSDictionary *)data {
|
|
|
weakify(self);
|
|
|
[self.eventDataSession getbaiduNetStatusWithCompletionBlock:^(NSTimeInterval cts, NSInteger errCode) {
|
|
|
strongify(self);
|
|
|
NSMutableDictionary *appendDict = [[NSMutableDictionary alloc] initWithDictionary:data];
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%zd",errCode] forKey:YOHOAppReportKeyJsonKeyErrorTEC];
|
|
|
[appendDict setValue:[NSString stringWithFormat:@"%ld",(long)cts] forKey:YOHOAppReportKeyJsonKeyErrorCTS];
|
|
|
[eventDic setValue:appendDict forKey:YOHOAppReportKeyPARAM];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self pushData:eventDic dataType:YHEventReportDataTypeCustom];
|
|
|
});
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
- (void)pushCrashData:(NSDictionary*)data {
|
|
|
if (!IsDictionaryClass(data) || data.count <= 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
[eventDic addEntriesFromDictionary:data];
|
|
|
|
|
|
[self pushData:[eventDic copy] dataType:YHEventReportDataTypeCustom];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
NSMutableDictionary *eventDic = [[YH_EventDataFactory factoryEventAppInfo:self.appInfo] mutableCopy];
|
|
|
[eventDic addEntriesFromDictionary:data];
|
|
|
[self pushData:[eventDic copy] dataType:YHEventReportDataTypeCustom];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
#pragma mark - private
|
|
|
- (void)pushData:(NSDictionary*)data dataType:(YHEventReportDataType)type
|
|
|
{
|
|
|
if (type == YHEventReportDataTypeClickEvent) {
|
|
|
[self.eventDataSession uploadEventData:data tag:nil result:^(BOOL isSuc, id tag, NSError *error) {
|
|
|
}];
|
|
|
[self.eventDataSession uploadEventData:data tag:nil result:nil];
|
|
|
}else {
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
@synchronized (self.performanceCacheArray) {
|
|
|
[self.performanceCacheArray addObject:data];
|
|
|
}
|
|
|
[self.infoCacheArray addObject:data];
|
|
|
}else{
|
|
|
weakify(self);
|
|
|
[self.eventDataSession uploadPerformanceData:[self getPerformanceFormatData:@[data] type:YHEventReportTrackDataTypeNONE] tag:nil result:^(BOOL isSuc, id tag, NSError *error) {
|
|
|
if (!isSuc) {
|
|
|
strongify(self);
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.infoCacheArray addObject:data];
|
|
|
});
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void) startUploadMode:(YHEventReportStrategy)mode timeInterval:(long)timeIntervals;
|
|
|
- (void)startUploadMode:(YHEventReportStrategy)mode timeInterval:(NSUInteger)timeIntervals;
|
|
|
{
|
|
|
_mode = mode;
|
|
|
self.timeInterval = timeIntervals;
|
|
|
if (mode == YHEventReportStrategyInterval) {
|
|
|
if (self.timer == nil) {
|
|
|
//定时上传
|
|
|
self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode: NSRunLoopCommonModes];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
_mode = mode;
|
|
|
if (timeIntervals > 0 && timeIntervals < 5 * 60) {
|
|
|
self.timeInterval = timeIntervals;
|
|
|
}
|
|
|
}else{
|
|
|
if (self.timer) {
|
|
|
[self.timer invalidate];
|
|
|
self.timer = nil;
|
|
|
if (mode == YHEventReportStrategyInterval) {
|
|
|
[self startTimer];
|
|
|
}else{
|
|
|
[self cancelTimer];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (NSMutableDictionary*)getPerformanceFormatData:(NSArray*)dataArray type:(YHEventReportTrackDataType)type
|
...
|
...
|
@@ -278,135 +235,151 @@ static NSString *kParamKeyType = @"type"; |
|
|
}
|
|
|
|
|
|
[performanceAryDic setObject:[YH_EventDataFactory getDataType:type] forKey:kParamKeyType];
|
|
|
[performanceAryDic setObject:[YH_EventDataFactory getDeviceInfo:self.performanceAppInfo] forKey:kParamKeyDevice];
|
|
|
[performanceAryDic setObject:[YH_EventDataFactory getDeviceInfo:self.appInfo] forKey:kParamKeyDevice];
|
|
|
|
|
|
return performanceAryDic;
|
|
|
}
|
|
|
|
|
|
-(NSString *)getPointName:(YHEventReportPointName)pointName{
|
|
|
NSString *str = @"";
|
|
|
switch (pointName) {
|
|
|
case YHPN_VIEWCONTROLLER:
|
|
|
str = @"PAGE";
|
|
|
break;
|
|
|
case YHPN_WEBVIEW:
|
|
|
str = @"WEBVIEW";
|
|
|
break;
|
|
|
case YHPN_IMAGEVIEW:
|
|
|
str = @"IMAGE";
|
|
|
break;
|
|
|
case YHPN_BUTTON:
|
|
|
str = @"BUTTON";
|
|
|
break;
|
|
|
case YHPN_GESTURERECOGNIZER:
|
|
|
str = @"GESTURERECOGNIZER";
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
-(NSString *)getDataType:(YHEventReportTrackDataType)type{
|
|
|
NSString *str = @"";
|
|
|
|
|
|
switch (type) {
|
|
|
case YHEventReportTrackDataTypeStart:
|
|
|
str = @"start";
|
|
|
break;
|
|
|
case YHEventReportTrackDataTypeDestory:
|
|
|
str = @"destory";
|
|
|
break;
|
|
|
case YHEventReportTrackDataTypeStop:
|
|
|
str = @"stop";
|
|
|
break;
|
|
|
case YHEventReportTrackDataTypeHeartbeat:
|
|
|
str = @"hb";
|
|
|
break;
|
|
|
case YHEventReportTrackDataTypeNONE:
|
|
|
str = @"realTime";
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
- (void)refreshPushStatus
|
|
|
{
|
|
|
if (IOS10) {// system is iOS10
|
|
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
|
switch (settings.authorizationStatus) {
|
|
|
case UNAuthorizationStatusNotDetermined:
|
|
|
self.performanceAppInfo.ps = @"0";
|
|
|
break;
|
|
|
case UNAuthorizationStatusDenied:
|
|
|
self.performanceAppInfo.ps = @"0";
|
|
|
break;
|
|
|
default:
|
|
|
self.performanceAppInfo.ps = @"1";
|
|
|
break;
|
|
|
}
|
|
|
}];
|
|
|
} else {//iOS8
|
|
|
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
|
|
|
self.performanceAppInfo.ps = @"1";
|
|
|
}else {
|
|
|
self.performanceAppInfo.ps = @"0";
|
|
|
}
|
|
|
}
|
|
|
- (void)refreshAppInfo {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.appInfo update];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
#pragma mark - NSNotification
|
|
|
- (void)handleMemoryWarning
|
|
|
{
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
@synchronized (self.performanceCacheArray) {
|
|
|
[self.performanceCacheArray removeAllObjects];
|
|
|
dispatch_async(self.queue, ^{
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
//todo save to disk
|
|
|
[self.infoCacheArray removeAllObjects];
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- (void)handleDidFinishLaunch
|
|
|
{
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
[self uploadPerformanceDataWithType:YHEventReportTrackDataTypeStart];
|
|
|
}
|
|
|
dispatch_async(self.queue, ^{
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
[self loadDiskCachedEventInfoToMemArray:self.infoCacheArray];
|
|
|
[self clearDiskCache];
|
|
|
[self uploadEventAndCustomInfo:self.infoCacheArray type:YHEventReportTrackDataTypeStart completion:^(BOOL isSuccess) {
|
|
|
if (isSuccess) {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.infoCacheArray removeAllObjects];
|
|
|
});
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)handleTerminate
|
|
|
{
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
[self uploadPerformanceDataWithType:YHEventReportTrackDataTypeDestory];
|
|
|
}
|
|
|
dispatch_sync(self.queue, ^{// block until send over
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
NSArray *array = [self.infoCacheArray copy];
|
|
|
[self.infoCacheArray removeAllObjects];
|
|
|
[self saveToDiskOfInfo:array];
|
|
|
[self uploadEventAndCustomInfo:array type:YHEventReportTrackDataTypeDestory completion:^(BOOL isSuccess) {
|
|
|
if (isSuccess) {
|
|
|
[self clearDiskCache];
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)handleApplicationBackgrounding
|
|
|
{
|
|
|
[self.performanceAppInfo resetSessionID];
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
[self uploadPerformanceDataWithType:YHEventReportTrackDataTypeStop];
|
|
|
}
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.appInfo resetSessionID];
|
|
|
if (self.mode == YHEventReportStrategyInterval) {
|
|
|
NSArray *array = [self.infoCacheArray copy];
|
|
|
[self.infoCacheArray removeAllObjects];
|
|
|
[self saveToDiskOfInfo:array];
|
|
|
[self uploadEventAndCustomInfo:array type:YHEventReportTrackDataTypeStop completion:^(BOOL isSuccess) {
|
|
|
if (isSuccess) {
|
|
|
[self clearDiskCache];
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)handleApplicationForerounding {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self loadDiskCachedEventInfoToMemArray:self.infoCacheArray];
|
|
|
[self clearDiskCache];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- (void)networkStateChange:(NSNotification *)note {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.appInfo updateNetStatusWithReachability:self.intnetReachability];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
#pragma mark - timer
|
|
|
- (void)timerAction
|
|
|
{
|
|
|
[self uploadPerformanceDataWithType:YHEventReportTrackDataTypeHeartbeat];
|
|
|
|
|
|
- (void)startTimer {
|
|
|
if (!_timer) {
|
|
|
weakify(self);
|
|
|
_timer = yh_gcdTimer(_queue, _timeInterval, ^{
|
|
|
strongify(self);
|
|
|
[self timerAction];
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)cancelTimer {
|
|
|
if (_timer) {
|
|
|
dispatch_cancel(_timer);
|
|
|
_timer = nil;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)timerAction {
|
|
|
if (!self.infoCacheArray.count) {
|
|
|
return;
|
|
|
}
|
|
|
[self uploadEventAndCustomInfo:self.infoCacheArray type:YHEventReportTrackDataTypeHeartbeat completion:^(BOOL isSuccess) {
|
|
|
if (isSuccess) {
|
|
|
dispatch_async(self.queue, ^{
|
|
|
[self.infoCacheArray removeAllObjects];
|
|
|
});
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
#pragma mark - upload performance data
|
|
|
-(void)uploadPerformanceDataWithType:(YHEventReportTrackDataType)type{
|
|
|
@synchronized (self.performanceCacheArray) {
|
|
|
[self.eventDataSession uploadPerformanceData:[self getPerformanceFormatData:self.performanceCacheArray type:type] tag:nil result:^(BOOL isSuc, id tag, NSError *error) {
|
|
|
}];
|
|
|
-(void)uploadEventAndCustomInfo:(NSArray *)infoCacheArray type:(YHEventReportTrackDataType)type completion:(void(^)(BOOL isSuccess))completion {
|
|
|
[self.eventDataSession uploadPerformanceData:[self getPerformanceFormatData:infoCacheArray type:type] tag:nil result:^(BOOL isSuc, id tag, NSError *error) {
|
|
|
BLOCK_EXEC(completion, isSuc);
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
- (void)saveToDiskOfInfo:(NSArray *)infoArray {
|
|
|
[infoArray writeToFile:[self cachePath] atomically:YES];
|
|
|
}
|
|
|
|
|
|
- (void)loadDiskCachedEventInfoToMemArray:(NSMutableArray *)memArray {
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:[self cachePath]]) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@synchronized (self.performanceCacheArray) {
|
|
|
[self.performanceCacheArray removeAllObjects];
|
|
|
NSArray *array = [NSArray arrayWithContentsOfFile:[self cachePath]];
|
|
|
if (array.count) {
|
|
|
[memArray addObjectsFromArray:array];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)clearDiskCache {
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:[self cachePath]]) {
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:[self cachePath] error:nil];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (NSString *)cachePath {
|
|
|
return [NSTemporaryDirectory() stringByAppendingPathComponent:@"event.cache"];
|
|
|
}
|
|
|
|
|
|
@end |
...
|
...
|
|