Authored by 王钱钧

修改间隔发送逻辑

... ... @@ -19,8 +19,18 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[YH_Analytics shareInstance].logStrategy = LogStrategyImmedi;
// 设置发送策略
[YH_Analytics shareInstance].logStrategy = LogStrategyCustom;
// 自定义发送时间,从服务器获取
[YH_Analytics shareInstance].customInterval = 10;
// 设置用户id
[YH_Analytics shareInstance].uid = @"0000001";
// 启动
[[YH_Analytics shareInstance]startWithAppId:@"appid"];
return YES;
}
... ...
... ... @@ -70,6 +70,11 @@ typedef enum _YohoMobStatLogStrategy {
/**
* 间隔发送策略下,间隔
*/
@property (nonatomic) NSTimeInterval interval;
@property (nonatomic) NSTimeInterval customInterval;
/**
* 用户id
*/
@property (strong, nonatomic) NSString *uid;
@end
... ...
... ... @@ -25,12 +25,13 @@
#define kNetWorkStatus4G @"4"
#define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey"
#define kKeyUserDefaultsCustomInterval @"customInterval"
//阀值
#define kMaxLocalEventsCount 3000 // 本地持久化event最大条数
#define kMaxLocalRecoderFileSize (1024 * 30) // 本地持久化文件大小(3KB)
//#define kMaxInterval 10
#define kMinInterval 60
NSString * const JsonKeyData = @"data";
NSString * const JsonKeyDataTypeDevice = @"device";
... ... @@ -45,8 +46,8 @@ NSString * const JsonKeyDataTypeErrors = @"errors";
@property (strong, nonatomic) NSString *eventFilePath;
@property (strong, nonatomic) NSString *eventFileName;
@property (strong, nonatomic) NSString *session;
@property (strong, nonatomic) NSString *uid;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSTimeInterval interval;
@property (strong, nonatomic) NSMutableDictionary *immediUploadDataDic;
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的)
... ... @@ -132,16 +133,6 @@ static dispatch_queue_t persisitingQueue;
return [rawSession md5];
}
- (NSString *)uid
{
return @"00000001"; // 测试数据
}
- (NSTimeInterval)interval
{
return 60*60*24*7; // 7 days
}
#pragma mark - Functions
- (void)registerCrashReporter
... ... @@ -160,8 +151,23 @@ static dispatch_queue_t persisitingQueue;
if (self.logStrategy == LogStrategyAppLaunch) {
[self uploadDiskData];
} else if (self.logStrategy == LogStrategyCustom) {
// 获取上次启动时服务端给的时间间隔
if ([[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval]) {
self.interval = [[NSUserDefaults standardUserDefaults]doubleForKey:kKeyUserDefaultsCustomInterval];
}else {
self.interval = kMinInterval;
}
[self tryUploadDiskData];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(tryUploadDiskData) userInfo:nil repeats:YES];
// 从服务端获取新的发送策略,该策略在下一次APP启动后生效
if (self.customInterval) {
[[NSUserDefaults standardUserDefaults]setDouble:self.customInterval forKey:kKeyUserDefaultsCustomInterval];
}
}
}
... ... @@ -179,7 +185,7 @@ static dispatch_queue_t persisitingQueue;
YHEvent *event = [[YHEvent alloc]init];
event.op = eventId;
event.uid = self.uid;
event.uid = self.uid ? self.uid : @"";
event.ts = [self timestamp];
event.sid = self.session;
event.param = param;
... ... @@ -198,7 +204,7 @@ static dispatch_queue_t persisitingQueue;
{
YHError *error = [[YHError alloc]init];
error.et = errorType;
error.uid = self.uid;
error.uid = self.uid ? self.uid : @"";
error.ts = [self timestamp];
error.sid = self.session;
error.param = param;
... ... @@ -271,7 +277,7 @@ static dispatch_queue_t persisitingQueue;
if (success) {
// 当event数量大于阀值或者文件超过指定大小
// 当event数量大于阀值或者文件超过指定大小,上传本地数据
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.eventFileName error:NULL] fileSize];
if (self.allEventsCount > kMaxLocalEventsCount || fileSize > kMaxLocalRecoderFileSize) {
... ...