Commit c606fc11 authored by 井庆林's avatar 井庆林

..

parent 0323d362
...@@ -58,19 +58,26 @@ NSString *const MockCityId = @"beijing"; ...@@ -58,19 +58,26 @@ NSString *const MockCityId = @"beijing";
[dict setObject:@"" forKey:@"extra_param"]; [dict setObject:@"" forKey:@"extra_param"];
[dict setObject:@"" forKey:@"referrer_tab_name"]; [dict setObject:@"" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"]; [dict setObject:@(0) forKey:@"is_push"];
[Phobos track:@"page_view" attributes:dict]; // [Phobos track:@"page_view" attributes:dict];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
//
[Phobos track:@"page_view" attributes:dict]; // [Phobos track:@"page_view" attributes:dict];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[dict setObject:[PhobosUtil currentTime] forKey:@"in"]; [dict setObject:[PhobosUtil currentTime] forKey:@"in"];
[dict setObject:[PhobosUtil currentTime] forKey:@"out"]; [dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[Phobos track:@"page_view" attributes:dict sendNow:YES]; [Phobos track:@"page_view" attributes:dict sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[Phobos track:@"test" attributes:dict]; [Phobos track:@"test" attributes:dict sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; [Phobos track:@"test2" attributes:dict sendNow:YES];
[Phobos track:@"test3" attributes:dict sendNow:YES];
[Phobos track:@"test4" attributes:dict sendNow:YES];
[Phobos track:@"test3" attributes:dict sendNow:YES];
[Phobos track:@"test4" attributes:dict sendNow:YES];
[Phobos track:@"test3" attributes:dict sendNow:YES];
[Phobos track:@"test4" attributes:dict sendNow:YES];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
} }
......
...@@ -8,6 +8,7 @@ inhibit_all_warnings! ...@@ -8,6 +8,7 @@ inhibit_all_warnings!
target 'GMPhobos_Example' do target 'GMPhobos_Example' do
pod 'GMPhobos', :path => '../' pod 'GMPhobos', :path => '../'
# pod 'GMPhobos', :git => 'git@git.wanmeizhensuo.com:gengmeiios/GMPhobos.git', :branch => 'jql/coreData'
target 'GMPhobos_Tests' do target 'GMPhobos_Tests' do
inherit! :search_paths inherit! :search_paths
end end
......
...@@ -33,6 +33,6 @@ SPEC CHECKSUMS: ...@@ -33,6 +33,6 @@ SPEC CHECKSUMS:
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545 MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: 4db0de045b595396507d6bad04e4823b02c3061d PODFILE CHECKSUM: abbb682362e2d6dbd228843955b17f6796b958b0
COCOAPODS: 1.8.4 COCOAPODS: 1.8.4
This diff is collapsed.
...@@ -166,23 +166,13 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -166,23 +166,13 @@ NS_ASSUME_NONNULL_BEGIN
@interface NewPhobos (UtilTest) @interface NewPhobos (UtilTest)
/** 获取所有非立即发送埋点数量 */ /** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount; + (NSInteger)fetchToBeSendPhobosDataCount;
/** 获取所有非立即发送埋点数据 */ /** 获取发送埋点数据 */
+ (NSArray *)normalPhobosData; + (NSArray *)fetchToBeSendPhobosData;
/** 获取所有立即发送埋点数量 */ /** 清除待发送埋点数据缓存 */
+ (NSUInteger)immediatelyPhobosCount; + (void)removeAllPhobosData;
/** 获取所有立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosData;
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData;
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData;
@end @end
......
...@@ -386,15 +386,8 @@ static NSString *sdkVersion = @"1.4.0"; ...@@ -386,15 +386,8 @@ static NSString *sdkVersion = @"1.4.0";
NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:eventName attributes:attributes]; NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:eventName attributes:attributes];
@try { @try {
NSData *JSON = [PhobosUtil encodeJSON:dataDict]; NSData *JSON = [PhobosUtil encodeJSON:dataDict];
[PhobosDataManager insertData:dataDict sendAPI:currentAPI immediately:sendNow completion:^(BOOL contextDidSave, NSError * _Nullable error) { [PhobosDataManager insertData:dataDict sendAPI:currentAPI completion:^(BOOL contextDidSave, NSError * _Nullable error) {
if (sendNow) { [self disposeSendDataWithImmediately:sendNow];
[self disposeSendDataWithImmediately:YES];
} else {
NSArray<SendDataEntity *> *fetchArray = [PhobosDataManager fetchDataEntitiesWithImmediately:NO];
if (fetchArray.count > 50) {
[self disposeSendDataWithImmediately:NO];
}
}
}]; }];
} @catch (NSException *exception) { } @catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject "); NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
...@@ -405,8 +398,8 @@ static NSString *sdkVersion = @"1.4.0"; ...@@ -405,8 +398,8 @@ static NSString *sdkVersion = @"1.4.0";
* 处理发送数据 * 处理发送数据
*/ */
+ (void)disposeSendDataWithImmediately:(BOOL)immediately { + (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<SendDataEntity *> *entities = [PhobosDataManager fetchDataEntitiesWithImmediately:immediately]; NSArray<SendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
if (entities.count > 0) { if (immediately || entities.count > 50) {
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending completion:nil]; [PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending completion:nil];
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<SendDataEntity *> * _Nonnull finishEntities, NSInteger code) { [PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<SendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
if (code == 200) { if (code == 200) {
...@@ -425,37 +418,17 @@ static NSString *sdkVersion = @"1.4.0"; ...@@ -425,37 +418,17 @@ static NSString *sdkVersion = @"1.4.0";
@implementation NewPhobos (UtilTest) @implementation NewPhobos (UtilTest)
/** 获取所有非立即发送埋点数量 */ /** 获取所有非立即发送埋点数量 */
+ (NSInteger)normalPhobosCount { + (NSInteger)fetchToBeSendPhobosDataCount {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusNormal]; return [PhobosDataManager fetchCountOfToBeSendEntities];
return [PhobosDataManager fetchCountOfEntitiesWithPredicate:predicate];
}
/** 获取所有非立即发送埋点数据 */
+ (NSArray *)normalPhobosData {
return [PhobosDataManager fetchDataEntitiesWithImmediately:NO];
} }
/** 获取所有立即发送埋点数量 */ /** 获取待发送埋点数据 */
+ (NSUInteger)immediatelyPhobosCount { + (NSArray *)fetchToBeSendPhobosData {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError]; return [PhobosDataManager fetchToBeSendDataEntities];
return [PhobosDataManager fetchCountOfEntitiesWithPredicate:predicate];
}
/** 获取所有立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosData {
return [PhobosDataManager fetchDataEntitiesWithImmediately:YES];
}
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData {
NSArray<SendDataEntity *> *fetchArray = [PhobosDataManager fetchDataEntitiesWithImmediately:NO];
[PhobosDataManager deleteDataEntities:fetchArray];
} }
/** 清除立即发送埋点数据缓存 */ /** 清除待发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData { + (void)removeAllPhobosData {
NSArray<SendDataEntity *> *fetchArray = [PhobosDataManager fetchDataEntitiesWithImmediately:YES]; [PhobosDataManager deleteAllEntities];
[PhobosDataManager deleteDataEntities:fetchArray];
} }
@end @end
...@@ -160,25 +160,14 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -160,25 +160,14 @@ NS_ASSUME_NONNULL_BEGIN
@interface Phobos (UtilTest) @interface Phobos (UtilTest)
/** 获取所有非立即发送埋点数量 */ /** 获取发送埋点数量 */
+ (NSInteger)normalPhobosCount; + (NSInteger)fetchToBeSendPhobosDataCount;
/** 获取所有非立即发送埋点数据 */ /** 获取待发送埋点数据 */
+ (NSArray *)normalPhobosData; + (NSArray *)fetchToBeSendPhobosData;
/** 获取所有立即发送埋点数量 */
+ (NSUInteger)immediatelyPhobosCount;
/** 获取所有立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosData;
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData;
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData;
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
...@@ -212,47 +212,20 @@ static BOOL isGray = NO; ...@@ -212,47 +212,20 @@ static BOOL isGray = NO;
@implementation NewPhobos (UtilTest) @implementation NewPhobos (UtilTest)
/** 获取所有非立即发送埋点数量 */ /** 获取发送埋点数量 */
+ (NSInteger)normalPhobosCount { + (NSUInteger)fetchToBeSendPhobosDataCount {
return isGray ? [NewPhobos normalPhobosCount] : 0; return isGray ? [NewPhobos fetchToBeSendPhobosDataCount] : 0;
} }
/** 获取所有非立即发送埋点数据 */ /** 获取发送埋点数据 */
+ (NSArray *)normalPhobosData { + (NSArray *)fetchToBeSendPhobosData {
return isGray ? [NewPhobos normalPhobosData] : nil; return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
} }
/** 获取所有立即发送埋点数量 */ /** 清除待发送埋点数据缓存 */
+ (NSUInteger)immediatelyPhobosCount { + (void)removeAllPhobosData {
return isGray ? [NewPhobos immediatelyPhobosCount] : 0;
}
/** 获取所有立即发送埋点数据 */
+ (NSDictionary *)immediatelyPhobosData {
return isGray ? [NewPhobos immediatelyPhobosData] : nil;
}
/** 获取url的立即发送埋点数据 */
+ (NSArray *)immediatelyPhobosDataForURL:(NSString *)url {
return isGray ? [NewPhobos immediatelyPhobosDataForURL:url] : nil;
}
+ (NSUInteger)immediatelyPhobosCountForURL:(NSString *)url {
return isGray ? [NewPhobos immediatelyPhobosCountForURL:url] : 0;
}
/** 清除非立即发送埋点数据缓存 */
+ (void)removeAllNormalPhobosData {
if (isGray) {
[NewPhobos removeAllNormalPhobosData];
}
}
/** 清除立即发送埋点数据缓存 */
+ (void)removeAllImmediatelyPhobosData {
if (isGray) { if (isGray) {
[NewPhobos removeAllNormalPhobosData]; [NewPhobos removeAllPhobosData];
} }
} }
......
...@@ -21,12 +21,18 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) { ...@@ -21,12 +21,18 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject @interface PhobosDataManager : NSObject
+ (NSArray<SendDataEntity *> *)fetchDataEntitiesWithImmediately:(BOOL)immediately; /** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<SendDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<SendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter; + (NSArray<SendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSUInteger)fetchCountOfToBeSendEntities;
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter; + (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI immediately:(BOOL)immediately completion:(MRSaveCompletionHandler)completion;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion;
+ (void)updateDataEntities:(NSArray<SendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion; + (void)updateDataEntities:(NSArray<SendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
+ (void)deleteDataEntities:(NSArray<SendDataEntity *> *)entities; + (void)deleteDataEntities:(NSArray<SendDataEntity *> *)entities;
+ (void)deleteAllEntities;
@end @end
......
...@@ -25,16 +25,14 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -25,16 +25,14 @@ static NSManagedObjectContext *Phobos_defaultContext;
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model]; [NSManagedObjectModel MR_setDefaultManagedObjectModel:model];
[MagicalRecord setupAutoMigratingCoreDataStack]; [MagicalRecord setupAutoMigratingCoreDataStack];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
NSArray<SendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError completion:nil];
}); });
} }
+ (NSArray<SendDataEntity *> *)fetchDataEntitiesWithImmediately:(BOOL)immediately { + (NSArray<SendDataEntity *> *)fetchToBeSendDataEntities {
NSPredicate *predicate; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
if (immediately) {
predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
} else {
predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusNormal];
}
return [self fetchDataEntitiesWithPredicate:predicate]; return [self fetchDataEntitiesWithPredicate:predicate];
} }
...@@ -42,18 +40,23 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -42,18 +40,23 @@ static NSManagedObjectContext *Phobos_defaultContext;
return [SendDataEntity MR_findAllWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]]; return [SendDataEntity MR_findAllWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
} }
+ (NSUInteger)fetchCountOfToBeSendEntities {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
return [self fetchCountOfEntitiesWithPredicate:predicate];
}
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter { + (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [SendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]]; return [SendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
} }
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI immediately:(BOOL)immediately completion:(MRSaveCompletionHandler)completion { + (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion {
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) { if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return; return;
} }
SendDataEntity *entity = [SendDataEntity MR_createEntity]; SendDataEntity *entity = [SendDataEntity MR_createEntity];
entity.data = [data mj_JSONData]; entity.data = [data mj_JSONData];
entity.api = sendAPI; entity.api = sendAPI;
entity.status = immediately ? PhobosDataSendStatusToBeSend : PhobosDataSendStatusNormal; entity.status = PhobosDataSendStatusToBeSend;
entity.id = [PhobosUtil currentMMTime]; entity.id = [PhobosUtil currentMMTime];
[[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion]; [[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion];
} }
...@@ -76,4 +79,9 @@ static NSManagedObjectContext *Phobos_defaultContext; ...@@ -76,4 +79,9 @@ static NSManagedObjectContext *Phobos_defaultContext;
} }
} }
+ (void)deleteAllEntities {
[SendDataEntity MR_truncateAll];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
}
@end @end
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
}); });
} }
// TODO: 全量以后,一定时间需要清除下面逻辑
+ (void)disposeOldData { + (void)disposeOldData {
NSMutableArray *total = [NSMutableArray new]; NSMutableArray *total = [NSMutableArray new];
NSArray *normalDataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey]; NSArray *normalDataArray = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
...@@ -63,10 +64,12 @@ ...@@ -63,10 +64,12 @@
}]; }];
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<SendDataEntity *> *entities, BOOL * _Nonnull stop) { [sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<SendDataEntity *> *entities, BOOL * _Nonnull stop) {
// TODO: 如果数据量过大,
NSMutableArray *sendDatas = [NSMutableArray new]; NSMutableArray *sendDatas = [NSMutableArray new];
[entities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDatas addObject:[obj.data mj_JSONObject]]; [sendDatas addObject:[obj.data mj_JSONObject]];
}]; }];
if (sendDatas.count > 0) { if (sendDatas.count > 0) {
[self sendDataArray:sendDatas currentAPI:api success:^(NSInteger code) { [self sendDataArray:sendDatas currentAPI:api success:^(NSInteger code) {
if (completion) { if (completion) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment