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
fef3d62e
Commit
fef3d62e
authored
Apr 09, 2020
by
井庆林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code review
parent
331224cf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
11 deletions
+14
-11
PhobosDataManager.m
GMPhobos/Classes/PhobosDataManager.m
+14
-11
No files found.
GMPhobos/Classes/PhobosDataManager.m
View file @
fef3d62e
...
...
@@ -14,13 +14,16 @@
@implementation
PhobosDataManager
static
NSManagedObjectContext
*
Phobos_defaultContext
;
static
NSManagedObjectContext
*
PhobosDefaultContext
;
static
dispatch_queue_t
PhobosDataSaveQueue
;
+
(
void
)
initialize
{
+
(
void
)
load
{
static
dispatch_once_t
onceToken
;
dispatch_once
(
&
onceToken
,
^
{
PhobosDataSaveQueue
=
dispatch_queue_create
(
"dispatch_queue_phobos"
,
DISPATCH_QUEUE_SERIAL
);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
Phobos
_d
efaultContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSMainQueueConcurrencyType
];
Phobos
D
efaultContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSMainQueueConcurrencyType
];
NSBundle
*
bundle
=
[
NSBundle
bundleForClass
:[
self
class
]];
// 获取埋点数据的实体
NSURL
*
modelURL
=
[
bundle
URLForResource
:
@"GMPhobosData"
withExtension
:
@"momd"
];
...
...
@@ -36,7 +39,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
[
coordinator
addPersistentStoreWithType
:
NSSQLiteStoreType
configuration
:
nil
URL
:
[
NSURL
fileURLWithPath
:
dataPath
]
options
:
nil
error
:
nil
];
// 设置storeCoordinator
Phobos
_d
efaultContext
.
persistentStoreCoordinator
=
coordinator
;
Phobos
D
efaultContext
.
persistentStoreCoordinator
=
coordinator
;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate
*
predicate
=
[
NSPredicate
predicateWithFormat
:
@"status = %d"
,
PhobosDataSendStatusSending
];
...
...
@@ -56,7 +59,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+
(
NSArray
<
PhobosSendDataEntity
*>
*
)
fetchDataEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
{
return
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
Phobos
_d
efaultContext
];
return
[
PhobosSendDataEntity
MR_findAllWithPredicate
:
searchFilter
inContext
:
Phobos
D
efaultContext
];
}
+
(
NSUInteger
)
fetchCountOfToBeSendEntities
{
...
...
@@ -65,7 +68,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+
(
NSUInteger
)
fetchCountOfEntitiesWithPredicate
:
(
NSPredicate
*
)
searchFilter
{
return
[[
PhobosSendDataEntity
MR_numberOfEntitiesWithPredicate
:
searchFilter
inContext
:
Phobos
_d
efaultContext
]
integerValue
];
return
[[
PhobosSendDataEntity
MR_numberOfEntitiesWithPredicate
:
searchFilter
inContext
:
Phobos
D
efaultContext
]
integerValue
];
}
+
(
void
)
insertData
:
(
NSDictionary
*
)
data
sendAPI
:
(
NSString
*
)
sendAPI
{
...
...
@@ -76,7 +79,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
if
(
!
sendAPI
||
[
sendAPI
isEqualToString
:
@""
]
||
!
data
)
{
return
;
}
PhobosSendDataEntity
*
entity
=
[
PhobosSendDataEntity
MR_createEntityInContext
:
Phobos
_d
efaultContext
];
PhobosSendDataEntity
*
entity
=
[
PhobosSendDataEntity
MR_createEntityInContext
:
Phobos
D
efaultContext
];
entity
.
data
=
[
data
mj_JSONData
];
entity
.
api
=
sendAPI
;
entity
.
status
=
PhobosDataSendStatusToBeSend
;
...
...
@@ -100,14 +103,14 @@ static NSManagedObjectContext *Phobos_defaultContext;
+
(
void
)
deleteDataEntities
:
(
NSArray
<
PhobosSendDataEntity
*>
*
)
entities
{
if
(
entities
.
count
>
0
)
{
[
entities
enumerateObjectsUsingBlock
:
^
(
PhobosSendDataEntity
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
[
obj
MR_deleteEntityInContext
:
Phobos
_d
efaultContext
];
[
obj
MR_deleteEntityInContext
:
Phobos
D
efaultContext
];
}];
[
self
saveWithCompletion
:
nil
];
}
}
+
(
void
)
deleteAllEntities
{
[
PhobosSendDataEntity
MR_truncateAllInContext
:
Phobos
_d
efaultContext
];
[
PhobosSendDataEntity
MR_truncateAllInContext
:
Phobos
D
efaultContext
];
[
self
saveWithCompletion
:
nil
];
}
...
...
@@ -115,8 +118,8 @@ static NSManagedObjectContext *Phobos_defaultContext;
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/
+
(
void
)
saveWithCompletion
:
(
MRSaveCompletionHandler
)
completion
{
dispatch_async
(
dispatch_get_main_queue
()
,
^
{
[
Phobos
_d
efaultContext
MR_saveWithOptions
:
MRSaveSynchronously
completion
:
completion
];
dispatch_async
(
PhobosDataSaveQueue
,
^
{
[
Phobos
D
efaultContext
MR_saveWithOptions
:
MRSaveSynchronously
completion
:
completion
];
});
}
...
...
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