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
d83551e1
Commit
d83551e1
authored
Feb 04, 2020
by
井庆林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code review
parent
deb90c17
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
184 additions
and
0 deletions
+184
-0
PhobosDataManager.h
GMPhobos/Classes/PhobosDataManager.h
+43
-0
PhobosDataManager.m
GMPhobos/Classes/PhobosDataManager.m
+141
-0
No files found.
GMPhobos/Classes/PhobosDataManager.h
0 → 100644
View file @
d83551e1
//
// PhobosDataManager.h
// GMCache
//
// Created by Locus on 2020/1/21.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef
NS_ENUM
(
NSInteger
,
PhobosModelSendStatus
)
{
PhobosModelSendStatusNormal
=
0
,
PhobosModelSendStatusToBeSend
,
PhobosModelSendStatusSending
,
PhobosModelSendStatusFinish
,
PhobosModelSendStatusError
,
};
@interface
PhobosSendModel
:
NSObject
@property
(
nonatomic
,
copy
,
readonly
)
NSString
*
sendId
;
@property
(
nonatomic
,
strong
)
id
data
;
@property
(
nonatomic
,
copy
)
NSString
*
sendAPI
;
@property
(
nonatomic
,
assign
)
PhobosModelSendStatus
sendStatus
;
-
(
instancetype
)
initWithData
:(
id
)
data
sendAPI
:(
NSString
*
)
api
;
@end
@interface
PhobosDataManager
:
NSObject
+
(
void
)
insertData
:(
NSDictionary
*
)
data
sendAPI
:(
NSString
*
)
v
immediately
:(
BOOL
)
immediately
;
+
(
void
)
updateDataModel
:(
PhobosSendModel
*
)
model
sendStatus
:(
PhobosModelSendStatus
)
sendStatus
;
+
(
void
)
deleteDataModel
:(
PhobosSendModel
*
)
model
;
+
(
void
)
removeAllDataWithImmediately
:(
BOOL
)
immediately
;
+
(
NSArray
<
PhobosSendModel
*>
*
)
fetchDataModelsWithStatus
:(
PhobosModelSendStatus
)
stauts
;
+
(
void
)
sendData
;
@end
NS_ASSUME_NONNULL_END
GMPhobos/Classes/PhobosDataManager.m
0 → 100644
View file @
d83551e1
//
// PhobosDataManager.m
// GMCache
//
// Created by Locus on 2020/1/21.
//
#import "PhobosDataManager.h"
#import <GMCache/GMCache.h>
#import "PhobosUtil.h"
#import "PhobosConfig.h"
#import <MJExtension/MJExtension.h>
@implementation
PhobosSendModel
-
(
instancetype
)
initWithData
:(
id
)
data
sendAPI
:(
NSString
*
)
sendAPI
{
if
(
self
=
[
super
init
])
{
_data
=
data
;
_sendId
=
[
PhobosUtil
currentMMTime
];
_sendAPI
=
sendAPI
;
}
return
self
;
}
@end
@implementation
PhobosDataManager
+
(
NSArray
<
PhobosSendModel
*>
*
)
fetchCache
{
NSArray
*
data
=
[
GMCache
fetchObjectAtDocumentPathWithkey
:
PhobosNewCacheKey
];
NSArray
<
PhobosSendModel
*>
*
sendModelArray
=
[
PhobosSendModel
mj_objectArrayWithKeyValuesArray
:
data
];
return
sendModelArray
?:
[
NSArray
new
];
}
+
(
void
)
storeCache
:
(
NSArray
<
PhobosSendModel
*>
*
)
sendModelArray
{
NSArray
*
data
=
[
PhobosSendModel
mj_keyValuesArrayWithObjectArray
:
sendModelArray
];
[
GMCache
storeObjectAtDocumentPathWithkey
:
PhobosNewCacheKey
object
:
data
];
}
+
(
void
)
insertData
:
(
NSDictionary
*
)
data
sendAPI
:
(
NSString
*
)
sendAPI
immediately
:
(
BOOL
)
immediately
{
NSMutableArray
<
PhobosSendModel
*>
*
datas
=
[[
self
fetchCache
]
mutableCopy
];
PhobosSendModel
*
model
=
[[
PhobosSendModel
alloc
]
initWithData
:
data
sendAPI
:
sendAPI
];
model
.
sendStatus
=
immediately
?
PhobosModelSendStatusToBeSend
:
PhobosModelSendStatusNormal
;
[
datas
addObject
:
model
];
[
self
storeCache
:
datas
];
}
+
(
void
)
updateDataModel
:
(
PhobosSendModel
*
)
model
sendStatus
:
(
PhobosModelSendStatus
)
sendStatus
{
NSMutableArray
<
PhobosSendModel
*>
*
datas
=
[[
self
fetchCache
]
mutableCopy
];
__block
NSInteger
updateIdx
=
-
1
;
[
datas
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
_Nonnull
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
if
([
model
.
sendId
isEqualToString
:
obj
.
sendId
])
{
updateIdx
=
idx
;
model
.
sendStatus
=
sendStatus
;
*
stop
=
YES
;
}
}];
if
(
updateIdx
!=
-
1
)
{
[
datas
replaceObjectAtIndex
:
updateIdx
withObject
:
model
];
}
[
self
storeCache
:
datas
];
}
+
(
void
)
deleteDataModel
:
(
PhobosSendModel
*
)
model
{
NSMutableArray
<
PhobosSendModel
*>
*
datas
=
[[
self
fetchCache
]
mutableCopy
];
__block
NSInteger
updateIdx
=
-
1
;
[
datas
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
_Nonnull
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
if
([
model
.
sendId
isEqualToString
:
obj
.
sendId
])
{
updateIdx
=
idx
;
*
stop
=
YES
;
}
}];
[
datas
removeObjectAtIndex
:
updateIdx
];
[
self
storeCache
:
datas
];
}
+
(
void
)
removeAllDataWithImmediately
:
(
BOOL
)
immediately
{
NSArray
<
PhobosSendModel
*>
*
datas
=
[
self
fetchCache
];
NSMutableArray
<
PhobosSendModel
*>
*
temp
=
[
datas
mutableCopy
];
[
datas
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
_Nonnull
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
if
(
immediately
)
{
if
(
obj
.
sendStatus
==
PhobosModelSendStatusFinish
)
{
[
temp
removeObject
:
obj
];
}
}
else
{
if
(
obj
.
sendStatus
==
PhobosModelSendStatusNormal
)
{
obj
.
sendStatus
=
PhobosModelSendStatusToBeSend
;
[
temp
replaceObjectAtIndex
:
idx
withObject
:
obj
];
}
}
}];
[
self
storeCache
:
temp
];
}
+
(
NSArray
<
PhobosSendModel
*>
*
)
fetchDataModelsWithStatus
:
(
PhobosModelSendStatus
)
status
{
NSArray
<
PhobosSendModel
*>
*
data
=
[
self
fetchCache
];
NSMutableArray
*
temp
=
[
NSMutableArray
new
];
[
data
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
_Nonnull
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
if
(
obj
.
sendStatus
==
status
)
{
[
temp
addObject
:
obj
];
}
}];
return
temp
;
}
+
(
void
)
sendData
{
NSMutableDictionary
*
sendDataMap
=
[
NSMutableDictionary
new
];
NSArray
<
PhobosSendModel
*>
*
needSendData
=
[
self
fetchCache
];
[
needSendData
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
_Nonnull
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
if
(
obj
.
sendStatus
==
PhobosModelSendStatusToBeSend
||
obj
.
sendStatus
==
PhobosModelSendStatusError
)
{
NSMutableArray
*
temp
=
sendDataMap
[
obj
.
sendAPI
]
?:
[
NSMutableArray
new
];
[
temp
addObject
:
obj
];
[
sendDataMap
setValue
:
temp
forKey
:
obj
.
sendAPI
];
}
}];
[
sendDataMap
enumerateKeysAndObjectsUsingBlock
:
^
(
NSString
*
api
,
NSArray
<
PhobosSendModel
*>
*
data
,
BOOL
*
_Nonnull
stop
)
{
NSMutableArray
*
temp
=
[
NSMutableArray
new
];
[
data
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
[
self
updateDataModel
:
obj
sendStatus
:
PhobosModelSendStatusSending
];
[
temp
addObject
:
obj
.
data
];
}];
if
(
temp
.
count
>
0
)
{
NSData
*
JSON
=
[
PhobosUtil
encodeJSON
:
temp
];
NSData
*
compressedData
=
[
PhobosUtil
compressData
:
JSON
];
[
PhobosUtil
sendData
:
compressedData
currentAPI
:
api
success
:^
(
NSInteger
code
)
{
[
data
enumerateObjectsUsingBlock
:
^
(
PhobosSendModel
*
obj
,
NSUInteger
idx
,
BOOL
*
_Nonnull
stop
)
{
dispatch_async
(
dispatch_get_main_queue
(),
^
{
if
(
code
==
200
)
{
[
self
deleteDataModel
:
obj
];
}
else
{
[
self
updateDataModel
:
obj
sendStatus
:
PhobosModelSendStatusError
];
}
});
}];
}];
}
}];
}
@end
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