Commit fc15795a authored by 井庆林's avatar 井庆林

GMCoreData

parent e8865397
......@@ -440,6 +440,7 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-GMPhobos_Example/Pods-GMPhobos_Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/GMCache/GMCache.framework",
"${BUILT_PRODUCTS_DIR}/GMCoreData/GMCoreData.framework",
"${BUILT_PRODUCTS_DIR}/GMPhobos/GMPhobos.framework",
"${BUILT_PRODUCTS_DIR}/MJExtension/MJExtension.framework",
"${BUILT_PRODUCTS_DIR}/MagicalRecord/MagicalRecord.framework",
......@@ -448,6 +449,7 @@
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMCache.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMCoreData.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMPhobos.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MJExtension.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MagicalRecord.framework",
......
......@@ -8,6 +8,7 @@ inhibit_all_warnings!
target 'GMPhobos_Example' do
pod 'GMPhobos', :path => '../'
pod 'GMCoreData', '0.1.2'
# pod 'GMPhobos', :git => 'git@git.wanmeizhensuo.com:gengmeiios/GMPhobos.git', :branch => 'jql/coreData'
target 'GMPhobos_Tests' do
inherit! :search_paths
......
PODS:
- GMCache (1.0.1):
- TMCache (= 2.1.0)
- GMCoreData (0.1.2):
- MagicalRecord
- GMPhobos (1.4.0):
- GMCache
- MagicalRecord
- GMCoreData
- MJExtension
- MagicalRecord (2.3.2):
- MagicalRecord/Core (= 2.3.2)
......@@ -12,11 +14,13 @@ PODS:
- TMCache (2.1.0)
DEPENDENCIES:
- GMCoreData (= 0.1.2)
- GMPhobos (from `../`)
SPEC REPOS:
"git@git.wanmeizhensuo.com:gengmeiios/GMSpecs.git":
- GMCache
- GMCoreData
https://github.com/CocoaPods/Specs.git:
- MagicalRecord
- MJExtension
......@@ -28,11 +32,12 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMPhobos: 3934127a7ec536932be9a5e64562093d68f98edb
GMCoreData: fe7bb7a5f4bf4bb0e7d17abfcb33fcedd0e263c3
GMPhobos: ef443e816e2fde31e03b1895cc972441a992a1bc
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: abbb682362e2d6dbd228843955b17f6796b958b0
PODFILE CHECKSUM: 213d0e0dba68dae27c93eb4255b93490a6c22841
COCOAPODS: 1.8.4
COCOAPODS: 1.9.0
......@@ -26,7 +26,7 @@ Pod::Spec.new do |s|
s.source_files = 'GMPhobos/Classes/*.{h,m}'
s.dependency 'GMCache'
s.dependency 'MJExtension'
s.dependency 'MagicalRecord'
s.dependency 'GMCoreData'
s.library = 'z'
end
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="15702" systemVersion="19C57" minimumToolsVersion="Automatic" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="SendDataEntity" representedClassName="SendDataEntity" syncable="YES">
<attribute name="api" optional="YES" attributeType="String"/>
<attribute name="data" optional="YES" attributeType="Binary"/>
<attribute name="id" optional="YES" attributeType="String"/>
<attribute name="status" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
</entity>
<elements>
<element name="SendDataEntity" positionX="-45" positionY="0" width="128" height="103"/>
</elements>
</model>
\ No newline at end of file
......@@ -398,10 +398,10 @@ static NSString *sdkVersion = @"1.4.0";
* 处理发送数据
*/
+ (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<SendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
NSArray<PhobosDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
if (immediately || entities.count > PhobosShardCount) {
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending completion:nil];
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<SendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosDataEntity *> * _Nonnull finishEntities, NSInteger code) {
if (code == 200) {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusFinish completion:^(BOOL contextDidSave, NSError * _Nullable error) {
[PhobosDataManager deleteDataEntities:finishEntities];
......
......@@ -6,8 +6,7 @@
//
#import <Foundation/Foundation.h>
#import <MagicalRecord/MagicalRecord.h>
#import "SendDataEntity+CoreDataClass.h"
@import GMCoreData;
NS_ASSUME_NONNULL_BEGIN
......@@ -22,16 +21,16 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<SendDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<SendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSArray<PhobosDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<PhobosDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSUInteger)fetchCountOfToBeSendEntities;
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion;
+ (void)updateDataEntities:(NSArray<SendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
+ (void)updateDataEntities:(NSArray<PhobosDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
+ (void)deleteDataEntities:(NSArray<SendDataEntity *> *)entities;
+ (void)deleteDataEntities:(NSArray<PhobosDataEntity *> *)entities;
+ (void)deleteAllEntities;
@end
......
......@@ -10,8 +10,6 @@
#import "PhobosConfig.h"
#import "PhobosUtil.h"
#import <MJExtension/MJExtension.h>
#import "NSManagedObjectModel+MagicalRecord.h"
#import "NSPersistentStoreCoordinator+MagicalRecord.h"
@implementation PhobosDataManager
......@@ -20,24 +18,20 @@ static NSManagedObjectContext *Phobos_defaultContext;
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *modelURL = [bundle URLForResource:@"GMCoreData" withExtension:@"momd"];//NewCodeDataModel.xcdatamodeld
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model];
[MagicalRecord setupAutoMigratingCoreDataStack];
[GMCoreData setup];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
NSArray<SendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
NSArray<PhobosDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError completion:nil];
});
}
+ (NSArray<SendDataEntity *> *)fetchToBeSendDataEntities {
+ (NSArray<PhobosDataEntity *> *)fetchToBeSendDataEntities {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
return [self fetchDataEntitiesWithPredicate:predicate];
}
+ (NSArray<SendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [SendDataEntity MR_findAllWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
+ (NSArray<PhobosDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosDataEntity MR_findAllWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
}
+ (NSUInteger)fetchCountOfToBeSendEntities {
......@@ -46,14 +40,14 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [SendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
return [PhobosDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
}
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion {
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return;
}
SendDataEntity *entity = [SendDataEntity MR_createEntity];
PhobosDataEntity *entity = [PhobosDataEntity MR_createEntity];
entity.data = [data mj_JSONData];
entity.api = sendAPI;
entity.status = PhobosDataSendStatusToBeSend;
......@@ -61,18 +55,18 @@ static NSManagedObjectContext *Phobos_defaultContext;
[[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion];
}
+ (void)updateDataEntities:(NSArray<SendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
+ (void)updateDataEntities:(NSArray<PhobosDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
if (entities.count > 0) {
[entities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.status = sendStatus;
}];
[[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion];
}
}
+ (void)deleteDataEntities:(NSArray<SendDataEntity *> *)entities {
+ (void)deleteDataEntities:(NSArray<PhobosDataEntity *> *)entities {
if (entities.count > 0) {
[entities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntity];
}];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
......@@ -80,7 +74,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+ (void)deleteAllEntities {
[SendDataEntity MR_truncateAll];
[PhobosDataEntity MR_truncateAll];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
}
......
......@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface PhobosSendManager : NSObject
+ (void)sendDataWithEntities:(NSArray<SendDataEntity *> *)needSendData completion:(nullable void(^)(NSArray<SendDataEntity *> *finishEntities, NSInteger code))completion;
+ (void)sendDataWithEntities:(NSArray<PhobosDataEntity *> *)needSendData completion:(nullable void(^)(NSArray<PhobosDataEntity *> *finishEntities, NSInteger code))completion;
@end
......
......@@ -53,21 +53,21 @@
}
}
+ (void)sendDataWithEntities:(NSArray<SendDataEntity *> *)sendDataEntities completion:(void (^)(NSArray<SendDataEntity *> * _Nonnull, NSInteger))completion {
+ (void)sendDataWithEntities:(NSArray<PhobosDataEntity *> *)sendDataEntities completion:(void (^)(NSArray<PhobosDataEntity *> * _Nonnull, NSInteger))completion {
if (sendDataEntities.count == 0) {
return;
}
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
[sendDataEntities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDataEntities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSMutableArray *sendData = sendDataMap[obj.api] ?: [NSMutableArray new];
[sendData addObject:obj];
[sendDataMap setValue:sendData forKey:obj.api];
}];
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<SendDataEntity *> *entities, BOOL * _Nonnull stop) {
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosDataEntity *> *entities, BOOL * _Nonnull stop) {
// TODO: 如果数据量过大,
NSMutableArray *sendDatas = [NSMutableArray new];
[entities enumerateObjectsUsingBlock:^(SendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDatas addObject:[obj.data mj_JSONObject]];
}];
......
//
// SendDataEntity+CoreDataClass.h
// GMPhobos
//
// Created by Locus on 2020/3/5.
//
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
NS_ASSUME_NONNULL_BEGIN
@interface SendDataEntity : NSManagedObject
@end
NS_ASSUME_NONNULL_END
#import "SendDataEntity+CoreDataProperties.h"
//
// SendDataEntity+CoreDataClass.m
// GMPhobos
//
// Created by Locus on 2020/3/5.
//
//
#import "SendDataEntity+CoreDataClass.h"
@implementation SendDataEntity
@end
//
// SendDataEntity+CoreDataProperties.h
// GMPhobos
//
// Created by Locus on 2020/3/5.
//
//
#import "SendDataEntity+CoreDataClass.h"
NS_ASSUME_NONNULL_BEGIN
@interface SendDataEntity (CoreDataProperties)
+ (NSFetchRequest<SendDataEntity *> *)fetchRequest;
@property (nullable, nonatomic, copy) NSString *id;
@property (nullable, nonatomic, retain) NSData *data;
@property (nullable, nonatomic, copy) NSString *api;
@property (nonatomic) int16_t status;
@end
NS_ASSUME_NONNULL_END
//
// SendDataEntity+CoreDataProperties.m
// GMPhobos
//
// Created by Locus on 2020/3/5.
//
//
#import "SendDataEntity+CoreDataProperties.h"
@implementation SendDataEntity (CoreDataProperties)
+ (NSFetchRequest<SendDataEntity *> *)fetchRequest {
return [NSFetchRequest fetchRequestWithEntityName:@"SendDataEntity"];
}
@dynamic id;
@dynamic data;
@dynamic api;
@dynamic status;
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment