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

添加注释

parent 4e67fd77
......@@ -138,6 +138,10 @@ static BOOL isGray = NO;
[_oldPhobos onPVEnd:page];
}
/**
* 添加当前对象的成员变量的赋值,防止外部访问该数据,导致为nil,下同
*/
- (void)setCaptureNullExpection:(void (^)(NSString * _Nonnull, NSDictionary * _Nonnull))captureNullExpection {
_captureNullExpection = captureNullExpection;
_rePhobos.captureNullExpection = captureNullExpection;
......
......@@ -22,17 +22,45 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities;
/**
* 通过 searchFilter 获取数据
*/
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
/**
* 获取待发送和发送失败的数据数量
*/
+ (NSUInteger)fetchCountOfToBeSendEntities;
/**
* 通过 searchFilter 获取数据数量
*/
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
/**
* 插入埋点数据
* data 埋点参数
* sendAPI 数据接收的服务器API
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion;
/**
* 修改埋点数据
* entities 需要修改的实体
* sendStatus 需要修改为的状态,
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus;
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
/**
* 删除数据
*/
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities;
+ (void)deleteAllEntities;
......
......@@ -19,8 +19,10 @@ static NSManagedObjectContext *Phobos_defaultContext;
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
Phobos_defaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
// 获取埋点数据的实体
NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
......@@ -33,12 +35,15 @@ static NSManagedObjectContext *Phobos_defaultContext;
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil];
// 设置storeCoordinator
Phobos_defaultContext.persistentStoreCoordinator = coordinator;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
NSArray<PhobosSendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError];
/** 将发送成功的数据删除 */
NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish];
NSArray<PhobosSendDataEntity *> *finishEntities = [self fetchDataEntitiesWithPredicate:finishPredicate];
[self deleteDataEntities:finishEntities];
......@@ -106,6 +111,9 @@ static NSManagedObjectContext *Phobos_defaultContext;
[self saveWithCompletion:nil];
}
/**
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/
+ (void)saveWithCompletion:(MRSaveCompletionHandler)completion {
[Phobos_defaultContext MR_saveOnlySelfWithCompletion:completion];
}
......
......@@ -12,9 +12,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface PhobosSendManager : NSObject
/**
* 发送数据
*/
+ (void)sendDataWithEntities:(NSArray<PhobosSendDataEntity *> *)needSendData completion:(nullable void(^)(NSArray<PhobosSendDataEntity *> *finishEntities, NSInteger code))completion;
@end
NS_ASSUME_NONNULL_END
......@@ -23,6 +23,7 @@
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 处理非灰情况下未发送完成数据
[self disposeOldData];
});
}
......@@ -35,6 +36,7 @@
[total addObjectsFromArray:normalDataArray];
[total addObjectsFromArray:immiDataArray];
if (total.count > 0) {
/** 获取非灰度下所有普通埋点,进行发送 */
[self sendDataArray:total currentAPI:Phobos.sharedClient.serverAPI success:^(NSInteger code) {
if (code == 200) {
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
......@@ -45,6 +47,7 @@
NSString *exposureAPI = GMExactExposureApi;
NSArray *exposureArray = [GMCache fetchObjectAtDocumentPathWithkey:[PhobosUtil MD5String:exposureAPI]];
if (exposureArray.count > 0) {
/** 获取非灰度下发送失败的埋点,进行发送 */
[self sendDataArray:exposureArray currentAPI:exposureAPI success:^(NSInteger code) {
if (code = 200) {
[GMCache removeObjectAtDocumentPathWithkey:[PhobosUtil MD5String:exposureAPI]];
......@@ -57,9 +60,11 @@
if (sendDataEntities.count == 0) {
return;
} else if (sendDataEntities.count > PhobosMaxSendCount) {
/** 如果数据超过 PhobosMaxSendCount 数据规模,进行拆分,分批发送,默认100条 */
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:PhobosMaxSendCount];
[sendDataEntities enumerateObjectsUsingBlock:^(PhobosSendDataEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (temp.count >= PhobosMaxSendCount) {
// 会调用当前方法,但数据规模不会满足 > PhobosMaxSendCount,不会让数据走到下面的发送,拆分完成 会执行return
[self sendDataWithEntities:temp completion:completion];
[temp removeAllObjects];
}
......@@ -68,14 +73,15 @@
return;
}
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
// 将相同api的数据合并
[sendDataEntities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSMutableArray *sendData = sendDataMap[obj.api] ?: [NSMutableArray new];
[sendData addObject:obj];
[sendDataMap setValue:sendData forKey:obj.api];
}];
// 将实体转为待发送数据,取出PhobosSendDataEntity中的data,并发送
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosSendDataEntity *> *entities, BOOL * _Nonnull stop) {
// TODO: 如果数据量过大,
NSMutableArray *sendDatas = [NSMutableArray new];
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDatas addObject:[obj.data mj_JSONObject]];
......
......@@ -47,6 +47,7 @@
// 适用于链路中页面浏览事件的统计 添加 refererLink 参数
- (void)initRefererLink {
// 添加 self.referrerLink.count == 0 ,处理referrerLink有值,就不需要初始化,为后期push、UniversalLinks修改referrerLink进行扩展,否则会重新被赋值
if (self.referrerLink.count == 0 && [self isKindOfClass:[UIViewController class]]) {
// 分present与navigation两种情况
UIViewController *me = (UIViewController *)self;
......
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