Commit 4a58871b authored by yueming lu's avatar yueming lu

photo数据存储重构

parent 0caeec4a
......@@ -97,23 +97,24 @@ NSString *const MockCityId = @"beijing";
[Phobos track:@"计数发送" attributes:@{@"index":@(i)} sendNow:NO];
}
NSLog(@"&&&&&&&&&&&&&&&&&");
}
break;
case 3://@"瞬间发送",
{
[Phobos track:@"瞬间发送" attributes:@{@"index":@(0)} sendNow:YES];
}
break;
case 4://瞬间发送和计数发送
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i< 100; i++) {
[Phobos track:@"瞬间发送和计数发送" attributes:@{@"index":@(i)} sendNow:i % 9];
}
});
}
break;
case 5://清空数据
case 5://表中有多少条数据
{
[Phobos fetchToBeSendPhobosDataCount];
}
......@@ -134,13 +135,13 @@ NSString *const MockCityId = @"beijing";
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[PhobosUtil currentTime] forKey:@"out"];
[dict setObject:inDate forKey:@"in"];
[dict setObject:@"test" forKey:@"page_name"];
[dict setObject:@"" forKey:@"business_id"];
[dict setObject:@"" forKey:@"referrer"];
[dict setObject:@"home" forKey:@"page_name"];
[dict setObject:@"referrer" forKey:@"referrer"];
[dict setObject:@"referrer" forKey:@"referrer"];
[dict setObject:@(0) forKey:@"fake"];
[dict setObject:@"" forKey:@"referrer_id"];
[dict setObject:@"" forKey:@"extra_param"];
[dict setObject:@"" forKey:@"referrer_tab_name"];
[dict setObject:@"referrer_id" forKey:@"referrer_id"];
[dict setObject:@"extra_param" forKey:@"extra_param"];
[dict setObject:@"referrer_tab_name" forKey:@"referrer_tab_name"];
[dict setObject:@(0) forKey:@"is_push"];
NSString *name = [NSString stringWithFormat:@"page_view-%d", index];
[Phobos track:name attributes:dict];
......
......@@ -173,9 +173,6 @@ NS_ASSUME_NONNULL_BEGIN
/** 获取所有非立即发送埋点数量 */
+ (NSUInteger)fetchToBeSendPhobosDataCount;
/** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData;
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData;
......
......@@ -105,14 +105,14 @@ static NewPhobos *_sharedClient;
- (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType],
@"cpu_count": [PhobosUtil currentDeviceCPUCount],
@"mac_address": [PhobosUtil getMacAddress],
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[NewPhobos track:@"device_opened" attributes:dict sendNow:YES];
}
......@@ -435,11 +435,6 @@ static NewPhobos *_sharedClient;
return [[PhobosDataManager sharedPhobosDataManager] messageCount] ;
}
///** 获取待发送埋点数据, 用同步来保障异步获取数据 */
//+ (NSArray *)fetchToBeSendPhobosData {
// return [PhobosDataManager fetchToBeSendDataEntities];
//}
//
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData {
[[PhobosDataManager sharedPhobosDataManager] removeAll];
......
......@@ -98,14 +98,15 @@ static OldPhobos *sharedClient = nil;
- (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/
NSDictionary *dict = @{@"build_cpu_abi": [PhobosUtil currentDeviceCPUType],
@"cpu_count": [PhobosUtil currentDeviceCPUCount],
@"mac_address": [PhobosUtil getMacAddress],
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[OldPhobos track:@"device_opened" attributes:dict sendNow:YES];
}
......
......@@ -244,10 +244,10 @@ static BOOL isGray = NO;
return isGray ? [NewPhobos fetchToBeSendPhobosDataCount] : 0;
}
/** 获取待发送埋点数据 */
+ (NSArray *)fetchToBeSendPhobosData {
return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
}
///** 获取待发送埋点数据 */
//+ (NSArray *)fetchToBeSendPhobosData {
// return isGray ? [NewPhobos fetchToBeSendPhobosData] : @[];
//}
/** 清除待发送埋点数据缓存 */
+ (void)removeAllPhobosData {
......
//
// GMPhobosPageViewModel.h
// GMPhobos
//
// Created by edz on 2020/6/22.
//
#import "GMPhobosSqulitModel.h"
@interface GMPhobosPageViewModel : GMPhobosSqulitModel
@property (nonatomic, strong) NSString * gm_out;
@property (nonatomic, strong) NSString * gm_in;
@property (nonatomic, strong) NSString * gm_page_name;
@property (nonatomic, strong) NSString * gm_business_id;
@property (nonatomic, strong) NSString * gm_referrer;
@property (nonatomic, strong) NSString * gm_referrer_link;
@property (nonatomic, strong) NSString * gm_referrer_id;
@property (nonatomic, strong) NSString * gm_extra_param;
@property (nonatomic, strong) NSString * gm_referrer_tab_name;
@property (nonatomic, strong) NSString * gm_is_push;
@property (nonatomic, strong) NSString * gm_message_id;
@property (nonatomic, strong) NSString * gm_in_time_millis;
@property (nonatomic, strong) NSString * gm_out_time_millis;
@property (nonatomic, strong) NSString * gm_is_first;
+ (GMPhobosPageViewModel *)pageViewModelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status;
@end
//
// GMPhobosPageViewModel.m
// GMPhobos
//
// Created by edz on 2020/6/22.
//
#import "GMPhobosPageViewModel.h"
@implementation GMPhobosPageViewModel
+ (GMPhobosPageViewModel *)pageViewModelOfDict:(NSDictionary *)dict
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status {
NSDictionary *data = dict[@"params"];
GMPhobosPageViewModel *model = [[GMPhobosPageViewModel alloc] init];
//共有参数
model.tableName = name;
model.data = [GMPhobosTool dictionaryToJsonString:data];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
model.messageItemId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()];
model.status = PhobosDataSendStatus_wait;
model.phobosType = phobosType;
model.api = urlapi;
model.immediately = status;
//私有参数
model.gm_out = data[@"out"];
model.gm_in = data[@"in"];
model.gm_page_name = data[@"page_name"];
model.gm_business_id = data[@"business_id"];
model.gm_referrer = data[@"referrer"];
model.gm_referrer_link = data[@"referrer_link"];
model.gm_referrer_id = data[@"referrer_id"];
model.gm_extra_param = data[@"extra_param"];
model.gm_referrer_tab_name = data[@"referrer_tab_name"];
model.gm_is_push = data[@"is_push"];
model.gm_is_push = data[@"is_push"];
model.gm_message_id = data[@"message_id"];
model.gm_in_time_millis= data[@"in_time_millis"];
model.gm_out_time_millis = data[@"out_time_millis"];
model.gm_is_first = data[@"is_first"];
return model;
}
@end
......@@ -6,7 +6,8 @@
//
#import <Foundation/Foundation.h>
#import "GMPhobosTool.h"
#import <mach/mach_time.h>
typedef NS_ENUM(int, PhobosDataSendStatus) {
PhobosDataSendStatus_wait = 1, // 待发送数据
......@@ -19,13 +20,15 @@ typedef NS_ENUM(int, PhobosDataSendStatus) {
@interface GMPhobosSqulitModel : NSObject
/// 唯一标识(用于筛选)
@property (nonatomic, strong) NSString *messageId;
@property (nonatomic, strong) NSString *messageItemId;
/// 埋点类型
@property (nonatomic, strong) NSString *phobosType;
/// 内容
@property (nonatomic, strong) NSString *data;
/// 路径
@property (nonatomic, strong) NSString *api;
///插入表格名称
@property (nonatomic, strong) NSString *tableName;
/// 数据状态
@property (nonatomic, assign) PhobosDataSendStatus status;
///是否是立刻发的埋点
......@@ -34,6 +37,8 @@ typedef NS_ENUM(int, PhobosDataSendStatus) {
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
tableName:(NSString *)name
immediately:(BOOL)status;
@end
......@@ -6,17 +6,20 @@
//
#import "GMPhobosSqulitModel.h"
#import <mach/mach_time.h>
@implementation GMPhobosSqulitModel
+ (GMPhobosSqulitModel *)modelOfDict:(NSDictionary *)data
type:(NSString *)phobosType
urlApi:(NSString *)urlapi
immediately:(BOOL)status {
tableName:(NSString *)name
immediately:(BOOL)status{
GMPhobosSqulitModel *model = [GMPhobosSqulitModel new];
model.data = [GMPhobosSqulitModel dictionaryToJsonString:data];
model.tableName = name;
model.data = [GMPhobosTool dictionaryToJsonString:data];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
model.messageId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()];
model.messageItemId = [NSString stringWithFormat:@"%.f*%lld", time, mach_absolute_time()];
model.status = PhobosDataSendStatus_wait;
model.phobosType = phobosType;
model.api = urlapi;
......@@ -24,13 +27,5 @@
return model;
}
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
return nil;
}
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
//
// GMPhobosTool.h
// GMPhobos
//
// Created by edz on 2020/6/23.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface GMPhobosTool : NSObject
/// 拆分数组
/// @param array 原数组
/// @param subSize 每个数组的长度
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(int)subSize;
/// dictionary 转json
/// @param dict dict
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict;
@end
NS_ASSUME_NONNULL_END
//
// GMPhobosTool.m
// GMPhobos
//
// Created by edz on 2020/6/23.
//
#import "GMPhobosTool.h"
@implementation GMPhobosTool
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(int)subSize {
if (subSize < 1) return [NSArray new];
// 数组将被拆分成指定长度数组的个数
unsigned long count = array.count % subSize == 0 ? (array.count / subSize) : (array.count / subSize + 1);
// 用来保存指定长度数组的可变数组对象
NSMutableArray *arr = [[NSMutableArray alloc] init];
//利用总个数进行循环,将指定长度的元素加入数组
for (int i = 0; i < count; i ++) {
//数组下标
int index = i * subSize;
//保存拆分的固定长度的数组元素的可变数组
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
//移除子数组的所有元素
[arr1 removeAllObjects];
int j = index;
//将数组下标乘以1、2、3,得到拆分时数组的最大下标值,但最大不能超过数组的总大小
while (j < subSize*(i + 1) && j < array.count) {
[arr1 addObject:[array objectAtIndex:j]];
j += 1;
}
//将子数组添加到保存子数组的数组中
[arr addObject:[arr1 copy]];
}
return [arr copy];
}
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
return nil;
}
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
......@@ -7,6 +7,7 @@
//
#import <Foundation/Foundation.h>
#import <MJExtension/MJExtension.h>
// 数据库中常见的几种类型
#define SQL_TEXT @"TEXT" //文本
......@@ -16,9 +17,6 @@
@interface GMSQliteManager : NSObject
#pragma mark - 创建数据库
/// 创建数据库的单利方法
+ (instancetype)sharedSQliteManager;
#pragma mark - 创建表
/// 创建表
......
......@@ -23,34 +23,26 @@
@implementation GMSQliteManager
static GMSQliteManager *squliteManager = nil;
+ (instancetype)sharedSQliteManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[GMSQliteManager shareDatabase];
});
return squliteManager;
}
+ (instancetype)shareDatabase {
if (!squliteManager) {
- (instancetype)init
{
self = [super init];
if (self) {
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"GMFMDB.sqlite"];
NSLog(@"path------%@", path);
FMDatabase *fmdb = [FMDatabase databaseWithPath:path];
squliteManager = [GMSQliteManager new];
squliteManager.db = fmdb;
squliteManager.dbPath = path;
self.db = fmdb;
self.dbPath = path;
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
squliteManager.dbQueue = queue;
[squliteManager.db open];
self.dbQueue = queue;
[self.db open];
}
if (!squliteManager.db.isOpen) {
[squliteManager.db open];
if (!self.db.isOpen) {
[self.db open];
}
return squliteManager;
return self;
}
/// 创建表
- (BOOL)gm_createTable:(NSString *)tableName model:(Class)modelClass {
NSDictionary *dict = [self modelToDictionary:modelClass excludePropertyName:nil];
......@@ -79,7 +71,8 @@ static GMSQliteManager *squliteManager = nil;
/// 模型插入
- (BOOL)gm_insertTable:(NSString *)tableName model:(NSObject *)model {
//模型转字典
NSDictionary *dict = [self getModelPropertyKeyValue:model];
NSDictionary *dict = model.mj_keyValues;;
bool status = [self gm_insertTable:tableName dict:dict];
return status;
}
......@@ -176,7 +169,8 @@ static GMSQliteManager *squliteManager = nil;
if ([parameters isKindOfClass:[NSDictionary class]]) {
dict = parameters;
} else if([parameters isKindOfClass:[NSObject class]]) {
dict = [self getModelPropertyKeyValue:parameters];
NSObject *object =(NSObject *)parameters;
dict = object.mj_keyValues;
} else {
return NO;
}
......@@ -208,7 +202,9 @@ static GMSQliteManager *squliteManager = nil;
objectClass:(Class)cls
whereFormat:(NSString *)format {
NSDictionary *modlDict = [self modelToDictionary:cls excludePropertyName:filterArray];
return [self gm_lookupTable:tableName keyArray:modlDict.allKeys whereFormat:format];
NSArray *array = [self gm_lookupTable:tableName keyArray:modlDict.allKeys whereFormat:format];
NSMutableArray *modelArray = [cls mj_objectArrayWithKeyValuesArray:array];
return modelArray;
}
- (NSArray *)gm_lookupTable:(NSString *)tableName keyArray:(NSArray *)keyArray whereFormat:(NSString *)format {
......@@ -312,45 +308,27 @@ static GMSQliteManager *squliteManager = nil;
}];
}
#pragma mark - 模型转字典
- (NSDictionary *)getModelPropertyKeyValue:(id)model
{
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([model class], &outCount);
for (int i = 0; i < outCount; i++) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
id value = [model valueForKey:name];
if (value) {
[mDic setObject:value forKey:name];
}
}
free(properties);
return mDic;
}
#pragma mark - 通过类对象获取对象的Property的字典 key 是属性名称, value是属性的类型
- (NSDictionary *)modelToDictionary:(Class)cls excludePropertyName:(NSArray *)nameArr
{
if (cls == [NSObject class]) {
return [NSMutableDictionary dictionary];
}
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(cls, &outCount);
for (int i = 0; i < outCount; i++) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
if ([nameArr containsObject:name]) continue;
NSString *type = [NSString stringWithCString:property_getAttributes(properties[i]) encoding:NSUTF8StringEncoding];
id value = [self propertTypeConvert:type];
if (value) {
[mDic setObject:value forKey:name];
}
}
free(properties);
NSDictionary *subDict = [self modelToDictionary:[cls superclass] excludePropertyName:nameArr];
[mDic addEntriesFromDictionary:subDict];
return mDic;
}
......
......@@ -11,10 +11,15 @@
#import "PhobosUtil.h"
#import "GMSQliteManager.h"
#import "GMPhobosSqulitModel.h"
#import "GMPhobosPageViewModel.h"
#import <MJExtension/MJExtension.h>
#import "GMPhobosThread.h"
#import "GMPhobosTool.h"
#define KtableName @"phobosTable"
#define KPageViewTableName @"pageViewTable"
#define KName @"tableName"
#define KClass @"class"
@interface PhobosDataManager ()
//数据库操作对象(单利)
......@@ -23,6 +28,7 @@
@property (nonatomic, assign) NSInteger messageCont;
/// runloop线程,用来处理埋点的数据
@property (nonatomic, strong) GMPhobosThread *phobosThread;
@property (nonatomic, strong) NSArray *tableMessageArray;
@end
@implementation PhobosDataManager
......@@ -40,6 +46,16 @@ static PhobosDataManager *dataManager;
- (instancetype)init {
self = [super init];
if (self) {
_tableMessageArray = @[
@{
KName:KtableName,
KClass:[GMPhobosSqulitModel class]
},
@{
KName:KPageViewTableName,
KClass:[GMPhobosPageViewModel class]
}
];
[self setupSDK];
}
return self;
......@@ -51,20 +67,24 @@ static PhobosDataManager *dataManager;
- (void)remove {
bool status = [_squlitManager gm_deleteAllDataFromTable:KtableName];
NSLog(@"清空表格数状态:%d",status);
bool status1 = [_squlitManager gm_deleteAllDataFromTable:KPageViewTableName];
NSLog(@"清空表格数状态-KtableName:%d KPageViewTableName:%d",status,status1);
}
- (int)messageCount {
int count = [_squlitManager gm_tableItemCount:KtableName whereFormat:nil];
NSLog(@"数据总数:%d",count);
int count1 = [_squlitManager gm_tableItemCount:KPageViewTableName whereFormat:nil];
NSLog(@"数据总数:%d - KtableName:%d KPageViewTableName:%d", count + count1, count, count1);
return count;
}
#pragma mark - 初始化
- (void)setupSDK {
_squlitManager = [GMSQliteManager sharedSQliteManager];
//创建表格
bool status = [_squlitManager gm_createTable:KtableName model:[GMPhobosSqulitModel class]];
//创建数据库和表格
_squlitManager = [[GMSQliteManager alloc] init];
for (NSDictionary *dict in _tableMessageArray) {
[_squlitManager gm_createTable:dict[KName] model:dict[KClass]];
}
//初始化线程和runloop
__weak typeof(self) weakSelf = self;
_phobosThread = [[GMPhobosThread alloc] initWithBlock:^{
......@@ -81,15 +101,15 @@ static PhobosDataManager *dataManager;
#pragma mark - 方法只用在APP启动的时候
- (void)resetData {
for (NSDictionary *dict in _tableMessageArray) {
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSString *whereStr = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_send];
[_squlitManager gm_updateTable:KtableName dicOrModel:@{@"status":@(PhobosDataSendStatus_error)} whereFormat:whereStr];
[_squlitManager gm_updateTable:dict[KName] dicOrModel:@{@"status":@(PhobosDataSendStatus_error)} whereFormat:whereStr];
/** 将发送成功的数据删除 */
NSString *whereDelet = [NSString stringWithFormat:@"where status = %d", PhobosDataSendStatus_finish];
[_squlitManager gm_deleteTable:KtableName whereFormat:whereDelet];
/** 设置未发送数据数量 */
NSString *whereCount = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
_messageCont = [_squlitManager gm_tableItemCount:KtableName whereFormat:whereCount];
[_squlitManager gm_deleteTable:dict[KName] whereFormat:whereDelet];
}
}
#pragma mark - 插入数据
......@@ -98,12 +118,18 @@ static PhobosDataManager *dataManager;
phobosType:(NSString *)phoboType
immediately:(BOOL)status {
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) return;
GMPhobosSqulitModel *squlitModel = [GMPhobosSqulitModel modelOfDict:data type:phoboType urlApi:sendAPI immediately:status];
if ([phoboType containsString:@"page_view"]) {
GMPhobosPageViewModel *squlitModel = [GMPhobosPageViewModel pageViewModelOfDict:data type:phoboType urlApi:sendAPI tableName:KPageViewTableName immediately:status];
[self performSelector:@selector(insertModel:) onThread:_phobosThread withObject:squlitModel waitUntilDone:NO];
} else {
GMPhobosSqulitModel *squlitModel = [GMPhobosSqulitModel modelOfDict:data type:phoboType urlApi:sendAPI tableName:KtableName immediately:status];
[self performSelector:@selector(insertModel:) onThread:_phobosThread withObject:squlitModel waitUntilDone:NO];
}
}
- (void)insertModel:(GMPhobosSqulitModel *)squlitModel {
BOOL insertType = [_squlitManager gm_insertTable:KtableName model:squlitModel];
BOOL insertType = [_squlitManager gm_insertTable:squlitModel.tableName model:squlitModel];
if (!insertType) {//如果插入失败,就直接发送给服务器
[self sendModelArray:@[squlitModel]];
} else if (squlitModel.immediately) {//需要立刻发送的埋点
......@@ -112,26 +138,51 @@ static PhobosDataManager *dataManager;
self.messageCont++;
[self disposeSendDataWithImmediately:NO];
}
NSLog(@"插入数状态%d count:%d", insertType,_messageCont);
NSLog(@"插入%@状态%d count:%d",squlitModel.phobosType, insertType,_messageCont);
}
#pragma mark - 获取待发送数据并修改状态为“发送”
- (NSArray *)fetchToBeSendDataEntitiesAndUpdateWithSendStatus {
- (NSMutableArray *)fetchToBeSendDataEntitiesAndUpdateWithSendStatus {
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *dict in _tableMessageArray) {
/**获取待发送的数据*/
NSArray *array = [_squlitManager gm_lookupTable:KtableName filterArray:nil objectClass:[GMPhobosSqulitModel class] whereFormat:where];
NSArray *sub = [_squlitManager gm_lookupTable:dict[KName] filterArray:nil objectClass:dict[KClass] whereFormat:where];
[array addObjectsFromArray:sub];
/**将待发送的数据修改为“发送中”*/
[_squlitManager gm_updateTable:KtableName dicOrModel:@{@"status":@(PhobosDataSendStatus_send)} whereFormat:where];
[_squlitManager gm_updateTable:dict[KName] dicOrModel:@{@"status":@(PhobosDataSendStatus_send)} whereFormat:where];
}
return array;
}
//获取待发送的数据个数
- (int)getAllTabelNeedSendMessageCount {
int count = 0;
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
for (NSDictionary *dict in _tableMessageArray) {
int tableCount = [_squlitManager gm_tableItemCount:dict[KName] whereFormat:where];
count += tableCount;
}
return count;
}
#pragma mark - 是否需要发送数据
- (void)disposeSendDataWithImmediately:(BOOL)status {
NSString *where = [NSString stringWithFormat:@"where status = %d or status = %d", PhobosDataSendStatus_wait, PhobosDataSendStatus_error];
_messageCont = [_squlitManager gm_tableItemCount:KtableName whereFormat:where];
if (status || _messageCont >= PhobosShardCount) {
NSArray *array = [self fetchToBeSendDataEntitiesAndUpdateWithSendStatus];
NSMutableArray *entities = [GMPhobosSqulitModel mj_objectArrayWithKeyValuesArray:array];
if (!status) {
_messageCont = [self getAllTabelNeedSendMessageCount];
if (_messageCont < PhobosShardCount) return;
}
/**
能走到下面说明“满足上报埋点条件 status == yes || _messageCont >= PhobosShardCount”
*/
NSMutableArray *entities = [self fetchToBeSendDataEntitiesAndUpdateWithSendStatus];
if (entities.count >= PhobosMaxSendCount) { //大于100条 要对数据进行拆分
//拆分数组
NSArray *array = [GMPhobosTool splitArray:entities withSubSize:PhobosShardCount];
for (NSArray *sub in array) {
[self sendModelArray:sub];
}
} else {
[self sendModelArray:entities];
}
}
......@@ -147,8 +198,8 @@ static PhobosDataManager *dataManager;
#pragma mark - 上报数据成功
- (void)sendSuccess:(NSArray<GMPhobosSqulitModel*> *)entities {
for (GMPhobosSqulitModel *model in entities) {
NSString *whereStr = [NSString stringWithFormat:@"where messageId = '%@'", model.messageId];
bool status = [_squlitManager gm_deleteTable:KtableName whereFormat:whereStr];
NSString *whereStr = [NSString stringWithFormat:@"where messageItemId = '%@'", model.messageItemId];
bool status = [_squlitManager gm_deleteTable:model.tableName whereFormat:whereStr];
NSLog(@"删除状态:%d", status);
}
}
......@@ -156,8 +207,8 @@ static PhobosDataManager *dataManager;
#pragma mark - 上报数据失败
- (void)sendError:(NSArray<GMPhobosSqulitModel*> *)entities {
for (GMPhobosSqulitModel *model in entities) {
NSString *whereStr = [NSString stringWithFormat:@"where messageId = '%@'", model.messageId];
bool status = [_squlitManager gm_updateTable:KtableName
NSString *whereStr = [NSString stringWithFormat:@"where messageItemId = '%@'", model.messageItemId];
bool status = [_squlitManager gm_updateTable:model.tableName
dicOrModel: @{@"status":@(PhobosDataSendStatus_error)}
whereFormat:whereStr];
NSLog(@"发送失败后修改状态:%d",status);
......
......@@ -55,21 +55,8 @@
}
+ (void)sendDataWithEntities:(NSArray<GMPhobosSqulitModel *> *)sendDataEntities completion:(void (^)(NSArray<GMPhobosSqulitModel *> * _Nonnull, NSInteger))completion {
if (sendDataEntities.count == 0) {
return;
} else if (sendDataEntities.count > PhobosMaxSendCount) {
/** 如果数据超过 PhobosMaxSendCount 数据规模,进行拆分,分批发送,默认100条 */
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:PhobosMaxSendCount];
[sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (temp.count >= PhobosMaxSendCount) {
// 会调用当前方法,但数据规模不会满足 > PhobosMaxSendCount,不会让数据走到下面的发送,拆分完成 会执行return
[self sendDataWithEntities:temp completion:completion];
[temp removeAllObjects];
}
[temp addObject:obj];
}];
return;
}
if (sendDataEntities.count == 0) return;
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
// 将相同api的数据合并
[sendDataEntities enumerateObjectsUsingBlock:^(GMPhobosSqulitModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
......
......@@ -242,11 +242,11 @@
}
- (void)setMessageId:(NSString *)messageId {
objc_setAssociatedObject(self, @selector(messageId), messageId, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, @selector(messageItemId), messageId, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)messageId {
NSString *messageId = objc_getAssociatedObject(self, @selector(messageId));
NSString *messageId = objc_getAssociatedObject(self, @selector(messageItemId));
return messageId == nil ? @"" : messageId;
}
......
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