Skip to content

Commit

Permalink
Release 1.5.4
Browse files Browse the repository at this point in the history
1. Bug fix for SQLite
  • Loading branch information
Yuhan ZOU committed Jul 7, 2016
1 parent e37ce21 commit d13a46d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 62 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/SensorsAnalyticsSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SensorsAnalyticsSDK"
s.version = "1.5.3"
s.version = "1.5.4"
s.summary = "The offical iOS SDK of Sensors Analytics."
s.homepage = "http://www.sensorsdata.cn"
s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" }
Expand Down
11 changes: 0 additions & 11 deletions SensorsAnalyticsSDK/SensorsAnalyticsSDK/SensorsAnalyticsSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,6 @@ typedef NS_ENUM(NSInteger, SensorsAnalyticsDebugMode) {
*/
- (void)flush;

/**
* @abstract
* 当前有多少条记录在缓存中
*
* @discussion
* 这个函数并不是线程安全的,取值的同时可能被其它线程修改
*
* @return 当前的记录条数
*/
- (NSUInteger) currentQueueCount;

@end

/**
Expand Down
91 changes: 41 additions & 50 deletions SensorsAnalyticsSDK/SensorsAnalyticsSDK/SensorsAnalyticsSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#import "SASwizzler.h"
#import "SensorsAnalyticsSDK.h"

#define VERSION @"1.5.3"
#define VERSION @"1.5.4"

#define PROPERTY_LENGTH_LIMITATION 8191

Expand Down Expand Up @@ -245,36 +245,28 @@ - (instancetype)initWithServerURL:(NSString *)serverURL
}

- (void)flushByType:(NSString *)type withSize:(int)flushSize andFlushMethod:(BOOL (^)(NSArray *))flushMethod {
NSArray *recordArray = [self.messageQueue getFirstRecords:flushSize withType:type];
if (recordArray == nil) {
@throw [NSException exceptionWithName:@"SqliteException"
reason:@"getFirstRecords from Message Queue in Sqlite fail"
userInfo:nil];
}

while ([recordArray count] > 0) {
if (!flushMethod(recordArray)) {
while (true) {
NSArray *recordArray = [self.messageQueue getFirstRecords:flushSize withType:type];
if (recordArray == nil) {
SAError(@"Failed to get records from SQLite.");
break;
}

if (![self.messageQueue removeFirstRecords:flushSize withType:type]) {
@throw [NSException exceptionWithName:@"SqliteException"
reason:@"removeFirstRecords from Message Queue in Sqlite fail"
userInfo:nil];
if ([recordArray count] == 0 || !flushMethod(recordArray)) {
break;
}

recordArray = [self.messageQueue getFirstRecords:flushSize withType:type];
if (recordArray == nil) {
@throw [NSException exceptionWithName:@"SqliteException"
reason:@"getFirstRecords from Message Queue in Sqlite fail"
userInfo:nil];
if (![self.messageQueue removeFirstRecords:flushSize withType:type]) {
SAError(@"Failed to remove records from SQLite.");
break;
}
SADebug(@"flush one batch success, currentCount is %lu", [self.messageQueue count]);

SADebug(@"flush one batch success.");
}
}

- (void)flush {
- (void)_flush {
SALog(@"flushing.");
// 使用 Post 发送数据
BOOL (^flushByPost)(NSArray *) = ^(NSArray *recordArray) {
// 1. 先完成这一系列Json字符串的拼接
Expand Down Expand Up @@ -448,14 +440,18 @@ - (void)flush {
#endif
return YES;
};

[self flushByType:@"Post" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushByPost];
[self flushByType:@"SFSafariViewController" withSize:50 andFlushMethod:flushBySafariVC];

if (![self.messageQueue vacuum]) {
SAError(@"Failed to VACUUM SQLite.");
}
}

dispatch_async(self.serialQueue, ^{
[self flushByType:@"Post" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushByPost];
[self flushByType:@"SFSafariViewController" withSize:(_debugMode == SensorsAnalyticsDebugOff ? 50 : 1) andFlushMethod:flushBySafariVC];

if (![self.messageQueue vacuum]) {
SAError(@"Failed to VACUUM SQLite.");
}
- (void)flush {
dispatch_sync(self.serialQueue, ^{
[self _flush];
});
}

Expand Down Expand Up @@ -497,25 +493,10 @@ - (void)enqueueWithType:(NSString *)type andEvent:(NSDictionary *)e {
[event setObject:libProperties forKey:@"lib"];
}

NSString *flushMethod = @"Post";
if ([properties objectForKey:@"$ios_install_source"]) {
flushMethod = @"SFSafariViewController";
}

[self.messageQueue addObejct:event withType:flushMethod];

if (_debugMode != SensorsAnalyticsDebugOff) {
// 在DEBUG模式下,直接发送事件
[self flush];
[self.messageQueue addObejct:event withType:@"SFSafariViewController"];
} else {
// 否则,在满足发送条件时,发送事件
if ([type isEqualToString:@"track_signup"] || [[self messageQueue] count] >= self.flushBulkSize) {
// 2. 判断当前网络类型是否是3G/4G/WIFI
NSString *networkType = [SensorsAnalyticsSDK getNetWorkStates];
if (![networkType isEqualToString:@"NULL"] && ![networkType isEqualToString:@"2G"]) {
[self flush];
}
}
[self.messageQueue addObejct:event withType:@"Post"];
}
}

Expand Down Expand Up @@ -655,6 +636,20 @@ - (void)track:(NSString *)event withProperties:(NSDictionary *)propertieDict wit
}

[self enqueueWithType:type andEvent:[e copy]];

if (_debugMode != SensorsAnalyticsDebugOff) {
// 在DEBUG模式下,直接发送事件
[self _flush];
} else {
// 否则,在满足发送条件时,发送事件
if ([type isEqualToString:@"track_signup"] || [[self messageQueue] count] >= self.flushBulkSize) {
// 2. 判断当前网络类型是否是3G/4G/WIFI
NSString *networkType = [SensorsAnalyticsSDK getNetWorkStates];
if (![networkType isEqualToString:@"NULL"] && ![networkType isEqualToString:@"2G"]) {
[self _flush];
}
}
}
});
}

Expand Down Expand Up @@ -960,10 +955,6 @@ - (NSDictionary *)currentSuperProperties {
return [_superProperties copy];
}

- (NSUInteger) currentQueueCount {
return [self.messageQueue count];
}

#pragma mark - Local caches

- (void)unarchive {
Expand Down

0 comments on commit d13a46d

Please sign in to comment.