Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
GMPhobos
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gengmeiios
GMPhobos
Commits
2d57d66e
Commit
2d57d66e
authored
Apr 28, 2020
by
井庆林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
19039216
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
44 additions
and
37 deletions
+44
-37
GMViewController.m
Example/GMPhobos/GMViewController.m
+13
-9
NewPhobos.m
GMPhobos/Classes/NewPhobos.m
+19
-13
Phobos.m
GMPhobos/Classes/Phobos.m
+2
-2
PhobosDataManager.h
GMPhobos/Classes/PhobosDataManager.h
+10
-13
PhobosDataManager.m
GMPhobos/Classes/PhobosDataManager.m
+0
-0
No files found.
Example/GMPhobos/GMViewController.m
View file @
2d57d66e
...
@@ -66,15 +66,19 @@ NSString *const MockCityId = @"beijing";
...
@@ -66,15 +66,19 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict];
// [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
for
(
int
i
=
0
;
i
<
200
;
i
++
)
{
// for (int i = 0; i < 200; i++) {
[
Phobos
track
:[
NSString
stringWithFormat
:
@"tt-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
// [Phobos track:[NSString stringWithFormat:@"phobos>>>tt-%d", i] attributes:dict sendNow:YES];
[
Phobos
track
:[
NSString
stringWithFormat
:
@"pv-%d"
,
i
]
attributes
:
dict
sendNow
:
(
arc4random
()
%
2
==
0
)];
// [Phobos track:[NSString stringWithFormat:@"phobos>>>pv-%d", i] attributes:dict sendNow:(arc4random() % 2 == 0)];
}
// NSUInteger count = [Phobos fetchToBeSendPhobosDataCount];
for
(
int
i
=
5
;
i
<
5
;
i
++
)
{
// NSLog(@"%lu", (unsigned long)count);
dispatch_async
(
dispatch_get_global_queue
(
0
,
0
),
^
{
// }
[
Phobos
track
:[
NSString
stringWithFormat
:
@"ay-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
// for (int i = 0; i < 200; i++) {
});
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
}
// NSUInteger count = [Phobos fetchToBeSendPhobosDataCount];
// NSLog(@"%lu", (unsigned long)count);
// [Phobos track:[NSString stringWithFormat:@"phobos>>>ay-%d", i] attributes:dict sendNow:YES];
// });
// }
}
}
-
(
void
)
touchesBegan
:
(
NSSet
<
UITouch
*>
*
)
touches
withEvent
:
(
UIEvent
*
)
event
{
-
(
void
)
touchesBegan
:
(
NSSet
<
UITouch
*>
*
)
touches
withEvent
:
(
UIEvent
*
)
event
{
...
...
GMPhobos/Classes/NewPhobos.m
View file @
2d57d66e
...
@@ -32,8 +32,6 @@ static NewPhobos *_sharedClient;
...
@@ -32,8 +32,6 @@ static NewPhobos *_sharedClient;
@implementation
NewPhobos
@implementation
NewPhobos
static
dispatch_semaphore_t
_phobos_semaphore
;
+
(
NewPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
+
(
NewPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
NewPhobos
.
sharedClient
.
appName
=
appName
;
NewPhobos
.
sharedClient
.
appName
=
appName
;
NewPhobos
.
sharedClient
.
channelId
=
channelId
;
NewPhobos
.
sharedClient
.
channelId
=
channelId
;
...
@@ -42,7 +40,6 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -42,7 +40,6 @@ static dispatch_semaphore_t _phobos_semaphore;
-
(
instancetype
)
init
{
-
(
instancetype
)
init
{
if
(
self
=
[
super
init
])
{
if
(
self
=
[
super
init
])
{
_phobos_semaphore
=
dispatch_semaphore_create
(
1
);
_appName
=
@""
;
_appName
=
@""
;
_channelId
=
@""
;
_channelId
=
@""
;
_logEnabled
=
NO
;
_logEnabled
=
NO
;
...
@@ -218,7 +215,7 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -218,7 +215,7 @@ static dispatch_semaphore_t _phobos_semaphore;
@"uuid"
:
[
PhobosUtil
deviceId
],
@"uuid"
:
[
PhobosUtil
deviceId
],
@"build_version_release"
:
[[
UIDevice
currentDevice
]
systemVersion
],
@"build_version_release"
:
[[
UIDevice
currentDevice
]
systemVersion
],
};
};
[
NewPhobos
track
:
@"on_app_session_over"
a
ttributes
:
dict
];
[
NewPhobos
track
SessionOverWithA
ttributes
:
dict
];
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosBeginTime
];
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosBeginTime
];
//当前session结束之后,把id置为0
//当前session结束之后,把id置为0
_serialId
=
0
;
_serialId
=
0
;
...
@@ -328,7 +325,6 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -328,7 +325,6 @@ static dispatch_semaphore_t _phobos_semaphore;
[
deviceParams
setValue
:[
PhobosUtil
getIPAddress
:
YES
]
forKey
:
@"ip"
];
[
deviceParams
setValue
:[
PhobosUtil
getIPAddress
:
YES
]
forKey
:
@"ip"
];
[
deviceParams
setValue
:
PhobosSafeString
(
_networkStatus
)
forKey
:
@"net_type"
];
[
deviceParams
setValue
:
PhobosSafeString
(
_networkStatus
)
forKey
:
@"net_type"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosUtil
platform
])
forKey
:
@"model"
];
[
deviceParams
setValue
:
PhobosSafeString
([
PhobosUtil
platform
])
forKey
:
@"model"
];
[
deviceParams
setValue
:
@
(
_isGray
)
forKey
:
@"isGray"
];
[
deviceParams
setValue
:
PhobosSafeString
([
UIDevice
currentDevice
].
systemVersion
)
forKey
:
@"sys_version"
];
[
deviceParams
setValue
:
PhobosSafeString
([
UIDevice
currentDevice
].
systemVersion
)
forKey
:
@"sys_version"
];
NSMutableDictionary
*
appParams
=
[
NSMutableDictionary
new
];
NSMutableDictionary
*
appParams
=
[
NSMutableDictionary
new
];
...
@@ -337,6 +333,7 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -337,6 +333,7 @@ static dispatch_semaphore_t _phobos_semaphore;
[
appParams
setValue
:
PhobosSafeString
(
_appVersion
)
forKey
:
@"version"
];
[
appParams
setValue
:
PhobosSafeString
(
_appVersion
)
forKey
:
@"version"
];
[
appParams
setValue
:
PhobosSafeString
(
_channelId
)
forKey
:
@"channel"
];
[
appParams
setValue
:
PhobosSafeString
(
_channelId
)
forKey
:
@"channel"
];
[
appParams
setValue
:
PhobosSafeString
(
_userType
)
forKey
:
@"user_type"
];
[
appParams
setValue
:
PhobosSafeString
(
_userType
)
forKey
:
@"user_type"
];
[
appParams
setValue
:
@
(
_isGray
)
forKey
:
@"is_gray"
];
[
appParams
setValue
:
PhobosSafeString
(
_currentCityId
)
forKey
:
@"current_city_id"
];
[
appParams
setValue
:
PhobosSafeString
(
_currentCityId
)
forKey
:
@"current_city_id"
];
[
appParams
setValue
:
@
(
_serialId
++
)
forKey
:
@"serial_id"
];
[
appParams
setValue
:
@
(
_serialId
++
)
forKey
:
@"serial_id"
];
...
@@ -381,11 +378,11 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -381,11 +378,11 @@ static dispatch_semaphore_t _phobos_semaphore;
}
}
+
(
void
)
onClickButtonWithAttributes
:
(
NSDictionary
*
)
attributes
{
+
(
void
)
onClickButtonWithAttributes
:
(
NSDictionary
*
)
attributes
{
[
self
track
:
@"on_cick_button"
attributes
:
attributes
];
[
self
track
:
@"on_c
l
ick_button"
attributes
:
attributes
];
}
}
+
(
void
)
onClickButtonWithAttributes
:
(
NSDictionary
*
)
attributes
sendNow
:
(
BOOL
)
sendNow
{
+
(
void
)
onClickButtonWithAttributes
:
(
NSDictionary
*
)
attributes
sendNow
:
(
BOOL
)
sendNow
{
[
self
track
:
@"on_cick_button"
attributes
:
attributes
sendNow
:
sendNow
];
[
self
track
:
@"on_c
l
ick_button"
attributes
:
attributes
sendNow
:
sendNow
];
}
}
+
(
void
)
track
:
(
NSString
*
)
eventName
{
+
(
void
)
track
:
(
NSString
*
)
eventName
{
...
@@ -412,25 +409,34 @@ static dispatch_semaphore_t _phobos_semaphore;
...
@@ -412,25 +409,34 @@ static dispatch_semaphore_t _phobos_semaphore;
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
];
[
PhobosDataManager
insertData
:
dataDict
sendAPI
:
currentAPI
completion
:^
(
BOOL
contextDidSave
,
NSError
*
_Nullable
error
)
{
[
self
disposeSendDataWithImmediately
:
sendNow
];
[
self
disposeSendDataWithImmediately
:
sendNow
];
}];
}
@catch
(
NSException
*
exception
)
{
}
@catch
(
NSException
*
exception
)
{
NSAssert
(
NO
,
@"哎呀呀,VALUE不能为NSObject "
);
NSAssert
(
NO
,
@"哎呀呀,VALUE不能为NSObject "
);
}
}
}
}
/**
* 为app_session_over提供埋点发送
* 其他情况在写入数据库成功,异步回调后在发送数据
* 这样可以优化发送次数,并能保障数据安全,但因为completion是使用主线程回调的,但进入后台后,主线程会休眠,所以单独处理该埋点
*/
+
(
void
)
trackSessionOverWithAttributes
:
(
NSDictionary
*
)
attributes
{
NSDictionary
*
dataDict
=
[
_sharedClient
prepareDictionaryForEvent
:
@"on_app_session_over"
attributes
:
attributes
];
[
PhobosDataManager
insertData
:
dataDict
sendAPI
:
_sharedClient
.
serverAPI
completion
:
nil
];
[
self
disposeSendDataWithImmediately
:
YES
];
}
/**
/**
* 处理发送数据
* 处理发送数据
*/
*/
+
(
void
)
disposeSendDataWithImmediately
:
(
BOOL
)
immediately
{
+
(
void
)
disposeSendDataWithImmediately
:
(
BOOL
)
immediately
{
NSInteger
count
=
[
PhobosDataManager
fetchCountOfToBeSendEntities
];
NSInteger
count
=
[
PhobosDataManager
fetchCountOfToBeSendEntities
];
if
(
immediately
||
count
>=
PhobosShardCount
)
{
if
(
immediately
||
count
>=
PhobosShardCount
)
{
dispatch_semaphore_wait
(
_phobos_semaphore
,
DISPATCH_TIME_FOREVER
);
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
PhobosDataManager
fetchToBeSendDataEntitiesAndUpdateWithSendStatus
:
PhobosDataSendStatusSending
];
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
PhobosDataManager
fetchToBeSendDataEntities
];
[
PhobosDataManager
updateDataEntities
:
entities
sendStatus
:
PhobosDataSendStatusSending
];
dispatch_semaphore_signal
(
_phobos_semaphore
);
[
PhobosSendManager
sendDataWithEntities
:
entities
completion
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
_Nonnull
finishEntities
,
NSInteger
code
)
{
[
PhobosSendManager
sendDataWithEntities
:
entities
completion
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
_Nonnull
finishEntities
,
NSInteger
code
)
{
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:(
code
==
200
?
PhobosDataSendStatusFinish
:
PhobosDataSendStatusError
)];
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:(
code
==
200
?
PhobosDataSendStatusFinish
:
PhobosDataSendStatusError
)
completion
:
nil
];
}];
}];
}
}
}
}
...
...
GMPhobos/Classes/Phobos.m
View file @
2d57d66e
...
@@ -65,7 +65,7 @@ static BOOL isGray = NO;
...
@@ -65,7 +65,7 @@ static BOOL isGray = NO;
if
(
isGray
)
{
if
(
isGray
)
{
[
NewPhobos
onClickButtonWithAttributes
:
attributes
];
[
NewPhobos
onClickButtonWithAttributes
:
attributes
];
}
else
{
}
else
{
[
self
track
:
@"on_cick_button"
attributes
:
attributes
];
[
self
track
:
@"on_c
l
ick_button"
attributes
:
attributes
];
}
}
}
}
...
@@ -73,7 +73,7 @@ static BOOL isGray = NO;
...
@@ -73,7 +73,7 @@ static BOOL isGray = NO;
if
(
isGray
)
{
if
(
isGray
)
{
[
NewPhobos
onClickButtonWithAttributes
:
attributes
sendNow
:
sendNow
];
[
NewPhobos
onClickButtonWithAttributes
:
attributes
sendNow
:
sendNow
];
}
else
{
}
else
{
[
self
track
:
@"on_cick_button"
attributes
:
attributes
sendNow
:
sendNow
];
[
self
track
:
@"on_c
l
ick_button"
attributes
:
attributes
sendNow
:
sendNow
];
}
}
}
}
...
...
GMPhobos/Classes/PhobosDataManager.h
View file @
2d57d66e
...
@@ -20,23 +20,22 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
...
@@ -20,23 +20,22 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface
PhobosDataManager
:
NSObject
@interface
PhobosDataManager
:
NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchToBeSendDataEntities
;
/**
/**
*
通过 searchFilter 获取数据
*
获取待发送和发送失败的数据数量
*/
*/
+
(
NS
Array
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:(
NSPredicate
*
)
searchFilter
;
+
(
NS
UInteger
)
fetchCountOfToBeSendEntities
;
/**
/**
* 获取待发送和发送失败的数据数量
* 获取待发送数据,包含待发送数据和发送失败数据
* predicate 通过谓词获取相应的数据
*/
*/
+
(
NSUInteger
)
fetchCountOfToBeSendEntities
;
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchToBeSendDataEntities
;
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:(
NSPredicate
*
)
predicate
;
/**
/**
*
通过 searchFilter 获取数据数量
*
获取待发送数据,包含待发送数据和发送失败数据, 并修改状态为sendStatus
*/
*/
+
(
NS
UInteger
)
fetchCountOfEntitiesWithPredicate
:(
NSPredicate
*
)
searchFilter
;
+
(
NS
Array
<
PhobosSendDataEntity
*>
*
)
fetchToBeSendDataEntitiesAndUpdateWithSendStatus
:(
PhobosDataSendStatus
)
sendStatus
;
/**
/**
* 插入埋点数据
* 插入埋点数据
...
@@ -45,7 +44,6 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
...
@@ -45,7 +44,6 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
*/
+
(
void
)
insertData
:(
NSDictionary
*
)
data
sendAPI
:(
NSString
*
)
sendAPI
;
+
(
void
)
insertData
:(
NSDictionary
*
)
data
sendAPI
:(
NSString
*
)
sendAPI
completion
:(
MRSaveCompletionHandler
)
completion
;
+
(
void
)
insertData
:(
NSDictionary
*
)
data
sendAPI
:(
NSString
*
)
sendAPI
completion
:(
MRSaveCompletionHandler
)
completion
;
/**
/**
...
@@ -55,13 +53,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
...
@@ -55,13 +53,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
*/
+
(
void
)
updateDataEntities
:(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
sendStatus
:(
PhobosDataSendStatus
)
sendStatus
;
+
(
void
)
updateDataEntities
:(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
sendStatus
:(
PhobosDataSendStatus
)
sendStatus
completion
:(
MRSaveCompletionHandler
)
completion
;
+
(
void
)
updateDataEntities
:(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
sendStatus
:(
PhobosDataSendStatus
)
sendStatus
completion
:(
MRSaveCompletionHandler
)
completion
;
/**
/**
* 删除数据
* 删除所有数据
* 仅限单元测试使用
*/
*/
+
(
void
)
deleteDataEntities
:(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
;
+
(
void
)
deleteAllEntities
;
+
(
void
)
deleteAllEntities
;
@end
@end
...
...
GMPhobos/Classes/PhobosDataManager.m
View file @
2d57d66e
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment