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
2bd3fb25
Commit
2bd3fb25
authored
Aug 08, 2019
by
井庆林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code review
parent
2814756f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
40 deletions
+48
-40
Phobos.m
GMPhobos/Classes/Phobos.m
+48
-40
No files found.
GMPhobos/Classes/Phobos.m
View file @
2bd3fb25
...
...
@@ -39,6 +39,8 @@ static NSString *sdkVersion = @"1.3.0";
static
dispatch_semaphore_t
_immediatelySemaphore
;
static
dispatch_semaphore_t
_normalSemaphore
;
static
dispatch_queue_t
_immediatelyQueue
;
static
dispatch_queue_t
_normalQueue
;
+
(
Phobos
*
)
clientWithAppName
:(
NSString
*
)
appName
channelId
:(
NSString
*
)
channelId
{
Phobos
.
sharedClient
.
appName
=
appName
;
...
...
@@ -51,6 +53,8 @@ static dispatch_semaphore_t _normalSemaphore;
dispatch_once
(
&
onceToken
,
^
{
_immediatelySemaphore
=
dispatch_semaphore_create
(
1
);
_normalSemaphore
=
dispatch_semaphore_create
(
1
);
_immediatelyQueue
=
dispatch_queue_create
(
"immdiately"
,
DISPATCH_QUEUE_CONCURRENT
);
_normalQueue
=
dispatch_queue_create
(
"normal"
,
DISPATCH_QUEUE_CONCURRENT
);
});
}
...
...
@@ -438,7 +442,7 @@ static dispatch_semaphore_t _normalSemaphore;
}
+
(
void
)
track
:
(
NSString
*
)
eventName
{
[
self
track
:
eventName
attributes
:
@{}
sendNow
:
NO
currentAPI
:
_sharedClient
.
serverAPI
];
[
self
track
:
eventName
attributes
:
nil
sendNow
:
NO
currentAPI
:
_sharedClient
.
serverAPI
];
}
+
(
void
)
track
:
(
NSString
*
)
eventName
attributes
:
(
NSDictionary
*
)
attributes
{
...
...
@@ -450,7 +454,7 @@ static dispatch_semaphore_t _normalSemaphore;
}
+
(
void
)
track
:
(
NSString
*
)
eventName
currentAPI
:
(
NSString
*
)
currentAPI
{
[
self
track
:
eventName
attributes
:
@{}
sendNow
:
NO
currentAPI
:
currentAPI
];
[
self
track
:
eventName
attributes
:
nil
sendNow
:
NO
currentAPI
:
currentAPI
];
}
+
(
void
)
track
:
(
NSString
*
)
eventName
attributes
:
(
NSDictionary
*
)
attributes
currentAPI
:
(
NSString
*
)
currentAPI
{
...
...
@@ -483,22 +487,24 @@ static dispatch_semaphore_t _normalSemaphore;
}
-
(
void
)
trackPhobosWithURL
:
(
NSString
*
)
url
data
:
(
NSDictionary
*
)
data
immediate
:
(
BOOL
)
immediate
{
dispatch_semaphore_wait
(
_normalSemaphore
,
DISPATCH_TIME_FOREVER
);
NSMutableDictionary
*
dataDict
=
[[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
]
mutableCopy
];
_normalCount
++
;
if
(
!
immediate
)
{
dataDict
=
[
self
dataDict
:
dataDict
setObject
:
data
forKey
:
url
];
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
object
:
dataDict
];
}
if
(
immediate
||
self
.
normalCount
>=
50
)
{
// 数据超过一定数量 或 进入后台等逻辑,统一进行发送普通埋点数据
[
dataDict
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
_Nonnull
key
,
NSArray
*
_Nonnull
obj
,
BOOL
*
_Nonnull
stop
)
{
if
(
obj
&&
obj
.
count
>
0
)
{
[
self
sendImmediatelyPhobosWithURL
:
key
data
:
obj
];
}
}];
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
];
}
dispatch_semaphore_signal
(
_normalSemaphore
);
dispatch_async
(
_normalQueue
,
^
{
dispatch_semaphore_wait
(
_normalSemaphore
,
DISPATCH_TIME_FOREVER
);
NSMutableDictionary
*
dataDict
=
[[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
]
mutableCopy
];
_normalCount
++
;
if
(
!
immediate
)
{
dataDict
=
[
self
dataDict
:
dataDict
setObject
:
data
forKey
:
url
];
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
object
:
dataDict
];
}
if
(
immediate
||
self
.
normalCount
>=
50
)
{
// 数据超过一定数量 或 进入后台等逻辑,统一进行发送普通埋点数据
[
dataDict
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
_Nonnull
key
,
NSArray
*
_Nonnull
obj
,
BOOL
*
_Nonnull
stop
)
{
if
(
obj
&&
obj
.
count
>
0
)
{
[
self
sendImmediatelyPhobosWithURL
:
key
data
:
obj
];
}
}];
[
GMCache
removeObjectAtDocumentPathWithkey
:
PhobosNormalCacheKey
];
}
dispatch_semaphore_signal
(
_normalSemaphore
);
});
}
/**
...
...
@@ -507,29 +513,31 @@ static dispatch_semaphore_t _normalSemaphore;
* @param data : 使用id类型是因为直接调用实时埋点传过来的是Dict,而普通埋点过来的则是Array类型
*/
-
(
void
)
sendImmediatelyPhobosWithURL
:
(
NSString
*
)
url
data
:
(
id
)
data
{
dispatch_semaphore_wait
(
_immediatelySemaphore
,
DISPATCH_TIME_FOREVER
);
__block
NSMutableDictionary
*
dataDict
=
[[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosImmediatelyCacheKey
]
mutableCopy
];
dataDict
=
[
self
dataDict
:
dataDict
setObject
:
data
forKey
:
url
];
NSInteger
allCount
=
dataDict
.
allKeys
.
count
;
// 请求个数
__block
finishCount
=
0
;
// 请求完成次数,不是请求成功次数,能在最后保证未发送成功的数据,能一次保存下来,等待下次发送
dispatch_semaphore_t
semaphore
=
dispatch_semaphore_create
(
1
);
// 因为网络请求是异步回调,需要保证数据安全
[
dataDict
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
_Nonnull
key
,
NSArray
*
_Nonnull
obj
,
BOOL
*
_Nonnull
stop
)
{
[
self
sendDataWithAPI
:
key
data
:
obj
successBlock
:^
(
BOOL
success
)
{
dispatch_semaphore_wait
(
semaphore
,
DISPATCH_TIME_FOREVER
);
finishCount
++
;
if
(
success
)
{
// 如果数据请求成功,删除当前数据的缓存
[
dataDict
removeObjectForKey
:
key
];
}
if
(
finishCount
==
allCount
)
{
// 所有数据都请求完成,重新进行数据缓存
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosImmediatelyCacheKey
object
:
dataDict
];
dispatch_semaphore_signal
(
_immediatelySemaphore
);
}
dispatch_semaphore_signal
(
semaphore
);
phobosLog
(
@"✈ ---------- ✈ data arrived Mars"
);
dispatch_async
(
_immediatelyQueue
,
^
{
dispatch_semaphore_wait
(
_immediatelySemaphore
,
DISPATCH_TIME_FOREVER
);
__block
NSMutableDictionary
*
dataDict
=
[[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosImmediatelyCacheKey
]
mutableCopy
];
dataDict
=
[
self
dataDict
:
dataDict
setObject
:
data
forKey
:
url
];
NSInteger
allCount
=
dataDict
.
allKeys
.
count
;
// 请求个数
__block
finishCount
=
0
;
// 请求完成次数,不是请求成功次数,能在最后保证未发送成功的数据,能一次保存下来,等待下次发送
dispatch_semaphore_t
semaphore
=
dispatch_semaphore_create
(
1
);
// 因为网络请求是异步回调,需要保证数据安全
[
dataDict
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
_Nonnull
key
,
NSArray
*
_Nonnull
obj
,
BOOL
*
_Nonnull
stop
)
{
[
self
sendDataWithAPI
:
key
data
:
obj
successBlock
:^
(
BOOL
success
)
{
dispatch_semaphore_wait
(
semaphore
,
DISPATCH_TIME_FOREVER
);
finishCount
++
;
if
(
success
)
{
// 如果数据请求成功,删除当前数据的缓存
[
dataDict
removeObjectForKey
:
key
];
}
if
(
finishCount
==
allCount
)
{
// 所有数据都请求完成,重新进行数据缓存
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosImmediatelyCacheKey
object
:
dataDict
];
dispatch_semaphore_signal
(
_immediatelySemaphore
);
}
dispatch_semaphore_signal
(
semaphore
);
phobosLog
(
@"✈ ---------- ✈ data arrived Mars"
);
}];
}];
}
]
;
}
)
;
}
/**
...
...
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