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

..

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