Authored by 王钱钧

修改间隔发送逻辑

@@ -19,8 +19,18 @@ @@ -19,8 +19,18 @@
19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20 // Override point for customization after application launch. 20 // Override point for customization after application launch.
21 21
22 - [YH_Analytics shareInstance].logStrategy = LogStrategyImmedi; 22 + // 设置发送策略
  23 + [YH_Analytics shareInstance].logStrategy = LogStrategyCustom;
  24 +
  25 + // 自定义发送时间,从服务器获取
  26 + [YH_Analytics shareInstance].customInterval = 10;
  27 +
  28 + // 设置用户id
  29 + [YH_Analytics shareInstance].uid = @"0000001";
  30 +
  31 + // 启动
23 [[YH_Analytics shareInstance]startWithAppId:@"appid"]; 32 [[YH_Analytics shareInstance]startWithAppId:@"appid"];
  33 +
24 return YES; 34 return YES;
25 } 35 }
26 36
@@ -70,6 +70,11 @@ typedef enum _YohoMobStatLogStrategy { @@ -70,6 +70,11 @@ typedef enum _YohoMobStatLogStrategy {
70 /** 70 /**
71 * 间隔发送策略下,间隔 71 * 间隔发送策略下,间隔
72 */ 72 */
73 -@property (nonatomic) NSTimeInterval interval; 73 +@property (nonatomic) NSTimeInterval customInterval;
  74 +
  75 +/**
  76 + * 用户id
  77 + */
  78 +@property (strong, nonatomic) NSString *uid;
74 79
75 @end 80 @end
@@ -25,12 +25,13 @@ @@ -25,12 +25,13 @@
25 #define kNetWorkStatus4G @"4" 25 #define kNetWorkStatus4G @"4"
26 26
27 #define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey" 27 #define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey"
  28 +#define kKeyUserDefaultsCustomInterval @"customInterval"
28 29
29 - 30 +//阀值
30 #define kMaxLocalEventsCount 3000 // 本地持久化event最大条数 31 #define kMaxLocalEventsCount 3000 // 本地持久化event最大条数
31 #define kMaxLocalRecoderFileSize (1024 * 30) // 本地持久化文件大小(3KB) 32 #define kMaxLocalRecoderFileSize (1024 * 30) // 本地持久化文件大小(3KB)
32 33
33 -//#define kMaxInterval 10 34 +#define kMinInterval 60
34 35
35 NSString * const JsonKeyData = @"data"; 36 NSString * const JsonKeyData = @"data";
36 NSString * const JsonKeyDataTypeDevice = @"device"; 37 NSString * const JsonKeyDataTypeDevice = @"device";
@@ -45,8 +46,8 @@ NSString * const JsonKeyDataTypeErrors = @"errors"; @@ -45,8 +46,8 @@ NSString * const JsonKeyDataTypeErrors = @"errors";
45 @property (strong, nonatomic) NSString *eventFilePath; 46 @property (strong, nonatomic) NSString *eventFilePath;
46 @property (strong, nonatomic) NSString *eventFileName; 47 @property (strong, nonatomic) NSString *eventFileName;
47 @property (strong, nonatomic) NSString *session; 48 @property (strong, nonatomic) NSString *session;
48 -@property (strong, nonatomic) NSString *uid;  
49 @property (strong, nonatomic) NSTimer *timer; 49 @property (strong, nonatomic) NSTimer *timer;
  50 +@property (assign, nonatomic) NSTimeInterval interval;
50 51
51 @property (strong, nonatomic) NSMutableDictionary *immediUploadDataDic; 52 @property (strong, nonatomic) NSMutableDictionary *immediUploadDataDic;
52 @property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的) 53 @property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的)
@@ -132,16 +133,6 @@ static dispatch_queue_t persisitingQueue; @@ -132,16 +133,6 @@ static dispatch_queue_t persisitingQueue;
132 return [rawSession md5]; 133 return [rawSession md5];
133 } 134 }
134 135
135 -- (NSString *)uid  
136 -{  
137 - return @"00000001"; // 测试数据  
138 -}  
139 -  
140 -- (NSTimeInterval)interval  
141 -{  
142 - return 60*60*24*7; // 7 days  
143 -}  
144 -  
145 #pragma mark - Functions 136 #pragma mark - Functions
146 137
147 - (void)registerCrashReporter 138 - (void)registerCrashReporter
@@ -160,8 +151,23 @@ static dispatch_queue_t persisitingQueue; @@ -160,8 +151,23 @@ static dispatch_queue_t persisitingQueue;
160 if (self.logStrategy == LogStrategyAppLaunch) { 151 if (self.logStrategy == LogStrategyAppLaunch) {
161 [self uploadDiskData]; 152 [self uploadDiskData];
162 } else if (self.logStrategy == LogStrategyCustom) { 153 } else if (self.logStrategy == LogStrategyCustom) {
  154 +
  155 + // 获取上次启动时服务端给的时间间隔
  156 + if ([[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval]) {
  157 + self.interval = [[NSUserDefaults standardUserDefaults]doubleForKey:kKeyUserDefaultsCustomInterval];
  158 + }else {
  159 + self.interval = kMinInterval;
  160 + }
  161 +
163 [self tryUploadDiskData]; 162 [self tryUploadDiskData];
  163 +
164 self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(tryUploadDiskData) userInfo:nil repeats:YES]; 164 self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(tryUploadDiskData) userInfo:nil repeats:YES];
  165 +
  166 + // 从服务端获取新的发送策略,该策略在下一次APP启动后生效
  167 + if (self.customInterval) {
  168 + [[NSUserDefaults standardUserDefaults]setDouble:self.customInterval forKey:kKeyUserDefaultsCustomInterval];
  169 + }
  170 +
165 } 171 }
166 } 172 }
167 173
@@ -179,7 +185,7 @@ static dispatch_queue_t persisitingQueue; @@ -179,7 +185,7 @@ static dispatch_queue_t persisitingQueue;
179 185
180 YHEvent *event = [[YHEvent alloc]init]; 186 YHEvent *event = [[YHEvent alloc]init];
181 event.op = eventId; 187 event.op = eventId;
182 - event.uid = self.uid; 188 + event.uid = self.uid ? self.uid : @"";
183 event.ts = [self timestamp]; 189 event.ts = [self timestamp];
184 event.sid = self.session; 190 event.sid = self.session;
185 event.param = param; 191 event.param = param;
@@ -198,7 +204,7 @@ static dispatch_queue_t persisitingQueue; @@ -198,7 +204,7 @@ static dispatch_queue_t persisitingQueue;
198 { 204 {
199 YHError *error = [[YHError alloc]init]; 205 YHError *error = [[YHError alloc]init];
200 error.et = errorType; 206 error.et = errorType;
201 - error.uid = self.uid; 207 + error.uid = self.uid ? self.uid : @"";
202 error.ts = [self timestamp]; 208 error.ts = [self timestamp];
203 error.sid = self.session; 209 error.sid = self.session;
204 error.param = param; 210 error.param = param;
@@ -271,7 +277,7 @@ static dispatch_queue_t persisitingQueue; @@ -271,7 +277,7 @@ static dispatch_queue_t persisitingQueue;
271 277
272 if (success) { 278 if (success) {
273 279
274 - // 当event数量大于阀值或者文件超过指定大小 280 + // 当event数量大于阀值或者文件超过指定大小,上传本地数据
275 unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.eventFileName error:NULL] fileSize]; 281 unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.eventFileName error:NULL] fileSize];
276 282
277 if (self.allEventsCount > kMaxLocalEventsCount || fileSize > kMaxLocalRecoderFileSize) { 283 if (self.allEventsCount > kMaxLocalEventsCount || fileSize > kMaxLocalRecoderFileSize) {