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
7e9ee617
Commit
7e9ee617
authored
Apr 11, 2020
by
井庆林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
ab34ff42
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
34 deletions
+21
-34
GMViewController.m
Example/GMPhobos/GMViewController.m
+1
-1
NewPhobos.m
GMPhobos/Classes/NewPhobos.m
+10
-13
PhobosDataManager.h
GMPhobos/Classes/PhobosDataManager.h
+2
-2
PhobosDataManager.m
GMPhobos/Classes/PhobosDataManager.m
+8
-18
No files found.
Example/GMPhobos/GMViewController.m
View file @
7e9ee617
...
...
@@ -66,7 +66,7 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
for
(
int
i
=
0
;
i
<
20
0
;
i
++
)
{
for
(
int
i
=
0
;
i
<
1
0
;
i
++
)
{
[
Phobos
track
:[
NSString
stringWithFormat
:
@"tt-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
[
Phobos
track
:[
NSString
stringWithFormat
:
@"pv-%d"
,
i
]
attributes
:
dict
sendNow
:
YES
];
}
...
...
GMPhobos/Classes/NewPhobos.m
View file @
7e9ee617
...
...
@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient;
@implementation
NewPhobos
static
dispatch_semaphore_t
_phobos_semaphore
;
+
(
NewPhobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
NewPhobos
.
sharedClient
.
appName
=
appName
;
NewPhobos
.
sharedClient
.
channelId
=
channelId
;
...
...
@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient;
-
(
instancetype
)
init
{
if
(
self
=
[
super
init
])
{
_phobos_semaphore
=
dispatch_semaphore_create
(
1
);
_appName
=
@""
;
_channelId
=
@""
;
_logEnabled
=
NO
;
...
...
@@ -422,11 +425,12 @@ static NewPhobos *_sharedClient;
+
(
void
)
disposeSendDataWithImmediately
:
(
BOOL
)
immediately
{
NSInteger
count
=
[
PhobosDataManager
fetchCountOfToBeSendEntities
];
if
(
immediately
||
count
>=
PhobosShardCount
)
{
[
PhobosDataManager
fetchToBeSendDataEntitiesWithEntitiesBlock
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
entities
)
{
[
PhobosDataManager
updateDataEntities
:
entities
sendStatus
:
PhobosDataSendStatusSending
];
[
PhobosSendManager
sendDataWithEntities
:
entities
completion
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
_Nonnull
finishEntities
,
NSInteger
code
)
{
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:(
code
==
200
?
PhobosDataSendStatusFinish
:
PhobosDataSendStatusError
)];
}];
dispatch_semaphore_wait
(
_phobos_semaphore
,
DISPATCH_TIME_FOREVER
);
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
)
{
[
PhobosDataManager
updateDataEntities
:
finishEntities
sendStatus
:(
code
==
200
?
PhobosDataSendStatusFinish
:
PhobosDataSendStatusError
)];
}];
}
}
...
...
@@ -442,14 +446,7 @@ static NewPhobos *_sharedClient;
/** 获取待发送埋点数据, 用同步来保障异步获取数据 */
+
(
NSArray
*
)
fetchToBeSendPhobosData
{
dispatch_semaphore_t
semaphore
=
dispatch_semaphore_create
(
0
);
__block
NSArray
<
PhobosSendDataEntity
*>
*
entityArray
;
[
PhobosDataManager
fetchToBeSendDataEntitiesWithEntitiesBlock
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
entities
)
{
entityArray
=
entities
;
dispatch_semaphore_signal
(
semaphore
);
}];
dispatch_semaphore_wait
(
semaphore
,
DISPATCH_TIME_FOREVER
);
return
entityArray
;
return
[
PhobosDataManager
fetchToBeSendDataEntities
];
}
/** 清除待发送埋点数据缓存 */
...
...
GMPhobos/Classes/PhobosDataManager.h
View file @
7e9ee617
...
...
@@ -21,12 +21,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface
PhobosDataManager
:
NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+
(
void
)
fetchToBeSendDataEntitiesWithEntitiesBlock
:(
nonnull
void
(
^
)(
NSArray
<
PhobosSendDataEntity
*>
*
entities
))
entitiesBlock
;
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchToBeSendDataEntities
;
/**
* 通过 searchFilter 获取数据
*/
+
(
void
)
fetchDataEntitiesWithPredicate
:(
NSPredicate
*
)
searchFilter
entitiesBlock
:(
nonnull
void
(
^
)(
NSArray
<
PhobosSendDataEntity
*>
*
entities
))
entitiesBlock
;
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:(
NSPredicate
*
)
searchFilter
;
/**
* 获取待发送和发送失败的数据数量
...
...
GMPhobos/Classes/PhobosDataManager.m
View file @
7e9ee617
...
...
@@ -15,15 +15,12 @@
@implementation
PhobosDataManager
static
NSManagedObjectContext
*
PhobosDefaultContext
;
static
dispatch_queue_t
PhobosDataSaveQueue
;
static
dispatch_semaphore_t
phobos_semaphore_t
;
static
dispatch_semaphore_t
phobos_rd_semaphore_t
;
+
(
void
)
initialize
{
static
dispatch_once_t
onceToken
;
dispatch_once
(
&
onceToken
,
^
{
phobos_semaphore_t
=
dispatch_semaphore_create
(
1
);
PhobosDataSaveQueue
=
dispatch_queue_create
(
"dispatch_rw_queue_phobos"
,
DISPATCH_QUEUE_CONCURRENT
);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
PhobosDefaultContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSPrivateQueueConcurrencyType
];
...
...
@@ -46,30 +43,23 @@ static dispatch_semaphore_t phobos_rd_semaphore_t;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate
*
predicate
=
[
NSPredicate
predicateWithFormat
:
@"status = %d"
,
PhobosDataSendStatusSending
];
[
self
fetchDataEntitiesWithPredicate
:
predicate
entitiesBlock
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
entities
)
{
[
self
updateDataEntities
:
entities
sendStatus
:
PhobosDataSendStatusError
];
}];
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
self
fetchDataEntitiesWithPredicate
:
predicate
];
[
self
updateDataEntities
:
entities
sendStatus
:
PhobosDataSendStatusError
];
/** 将发送成功的数据删除 */
NSPredicate
*
finishPredicate
=
[
NSPredicate
predicateWithFormat
:
@"status = %d"
,
PhobosDataSendStatusFinish
];
[
self
fetchDataEntitiesWithPredicate
:
finishPredicate
entitiesBlock
:
^
(
NSArray
<
PhobosSendDataEntity
*>
*
entities
)
{
[
self
deleteDataEntities
:
entities
];
}];
NSArray
<
PhobosSendDataEntity
*>
*
finishEntities
=
[
self
fetchDataEntitiesWithPredicate
:
finishPredicate
];
[
self
deleteDataEntities
:
finishEntities
];
});
}
+
(
void
)
fetchToBeSendDataEntitiesWithEntitiesBlock
:
(
void
(
^
)(
NSArray
<
PhobosSendDataEntity
*>
*
))
entitiesBlock
{
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchToBeSendDataEntities
{
NSPredicate
*
predicate
=
[
NSPredicate
predicateWithFormat
:
@"status = %d or status = %d"
,
PhobosDataSendStatusToBeSend
,
PhobosDataSendStatusError
];
[
self
fetchDataEntitiesWithPredicate
:
predicate
entitiesBlock
:
entitiesBlock
];
return
[
self
fetchDataEntitiesWithPredicate
:
predicate
];
}
+
(
void
)
fetchDataEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
entitiesBlock
:
(
nonnull
void
(
^
)(
NSArray
<
PhobosSendDataEntity
*>
*
))
entitiesBlock
{
dispatch_semaphore_wait
(
phobos_semaphore_t
,
DISPATCH_TIME_FOREVER
);
NSArray
<
PhobosSendDataEntity
*>
*
entities
=
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
PhobosDefaultContext
];
if
(
entitiesBlock
)
{
entitiesBlock
(
entities
);
}
dispatch_semaphore_signal
(
phobos_semaphore_t
);
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
{
return
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
PhobosDefaultContext
];
}
+
(
NSUInteger
)
fetchCountOfToBeSendEntities
{
...
...
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