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

采用多个CoreData方案

parent 85b3a77e
......@@ -440,7 +440,6 @@
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",
......@@ -449,7 +448,6 @@
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,8 +8,6 @@ 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
end
......
PODS:
- GMCache (1.0.1):
- TMCache (= 2.1.0)
- GMCoreData (0.1.2):
- MagicalRecord
- GMPhobos (1.4.0):
- GMCache
- GMCoreData
- MagicalRecord
- MJExtension
- MagicalRecord (2.3.2):
- MagicalRecord/Core (= 2.3.2)
......@@ -14,13 +12,11 @@ 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
......@@ -32,12 +28,11 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMCoreData: fe7bb7a5f4bf4bb0e7d17abfcb33fcedd0e263c3
GMPhobos: ef443e816e2fde31e03b1895cc972441a992a1bc
GMPhobos: d077fae9c149cf1b9658589a3fb9c26566066ad6
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: 213d0e0dba68dae27c93eb4255b93490a6c22841
PODFILE CHECKSUM: abbb682362e2d6dbd228843955b17f6796b958b0
COCOAPODS: 1.9.0
......@@ -26,7 +26,8 @@ Pod::Spec.new do |s|
s.source_files = 'GMPhobos/Classes/*.{h,m}'
s.dependency 'GMCache'
s.dependency 'MJExtension'
s.dependency 'GMCoreData'
s.dependency 'MagicalRecord'
s.library = 'z'
s.resource = 'GMPhobos/*.xcdatamodeld'
end
......@@ -401,10 +401,10 @@ static NSString *sdkVersion = @"1.4.0";
* 处理发送数据
*/
+ (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<PhobosDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
if (immediately || entities.count > PhobosShardCount) {
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending completion:nil];
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosDataEntity *> * _Nonnull finishEntities, NSInteger code) {
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
if (code == 200) {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusFinish completion:^(BOOL contextDidSave, NSError * _Nullable error) {
[PhobosDataManager deleteDataEntities:finishEntities];
......
......@@ -6,7 +6,8 @@
//
#import <Foundation/Foundation.h>
@import GMCoreData;
#import <MagicalRecord/MagicalRecord.h>
#import "PhobosSendDataEntity+CoreDataClass.h"
NS_ASSUME_NONNULL_BEGIN
......@@ -21,16 +22,16 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<PhobosDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<PhobosDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSUInteger)fetchCountOfToBeSendEntities;
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion;
+ (void)updateDataEntities:(NSArray<PhobosDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
+ (void)deleteDataEntities:(NSArray<PhobosDataEntity *> *)entities;
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities;
+ (void)deleteAllEntities;
@end
......
......@@ -10,6 +10,7 @@
#import "PhobosConfig.h"
#import "PhobosUtil.h"
#import <MJExtension/MJExtension.h>
#import <mach/mach_time.h>
@implementation PhobosDataManager
......@@ -18,20 +19,35 @@ static NSManagedObjectContext *Phobos_defaultContext;
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[GMCoreData setup];
Phobos_defaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
// 创建持久化存储调度器
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 创建并关联SQLite数据库文件,如果已经存在则不会重复创建
NSString *dataPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
dataPath = [dataPath stringByAppendingFormat:@"/%@.sqlite",@"GMPhobos"];
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil];
Phobos_defaultContext.persistentStoreCoordinator = coordinator;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
NSArray<PhobosDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
NSArray<PhobosSendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError completion:nil];
});
}
+ (NSArray<PhobosDataEntity *> *)fetchToBeSendDataEntities {
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
return [self fetchDataEntitiesWithPredicate:predicate];
}
+ (NSArray<PhobosDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosDataEntity MR_findAllWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:Phobos_defaultContext];
}
+ (NSUInteger)fetchCountOfToBeSendEntities {
......@@ -40,42 +56,42 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext MR_defaultContext]];
return [PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:Phobos_defaultContext];
}
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion {
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return;
}
PhobosDataEntity *entity = [PhobosDataEntity MR_createEntity];
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:Phobos_defaultContext];
entity.data = [data mj_JSONData];
entity.api = sendAPI;
entity.status = PhobosDataSendStatusToBeSend;
entity.id = [PhobosUtil currentMMTime];
[[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion];
entity.id = mach_absolute_time();
[Phobos_defaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion];
}
+ (void)updateDataEntities:(NSArray<PhobosDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
if (entities.count > 0) {
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.status = sendStatus;
}];
[[NSManagedObjectContext MR_defaultContext] MR_saveWithOptions:MRSaveSynchronously completion:completion];
[Phobos_defaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion];
}
}
+ (void)deleteDataEntities:(NSArray<PhobosDataEntity *> *)entities {
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities {
if (entities.count > 0) {
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntity];
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntityInContext:Phobos_defaultContext];
}];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
[Phobos_defaultContext MR_saveOnlySelfAndWait];
}
}
+ (void)deleteAllEntities {
[PhobosDataEntity MR_truncateAll];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
[PhobosSendDataEntity MR_truncateAllInContext:Phobos_defaultContext];
[Phobos_defaultContext MR_saveOnlySelfAndWait];
}
@end
//
// PhobosSendDataEntity+CoreDataClass.h
// GMPhobos
//
// Created by Locus on 2020/3/9.
//
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
NS_ASSUME_NONNULL_BEGIN
@interface PhobosSendDataEntity : NSManagedObject
@end
NS_ASSUME_NONNULL_END
#import "PhobosSendDataEntity+CoreDataProperties.h"
//
// PhobosSendDataEntity+CoreDataClass.m
// GMPhobos
//
// Created by Locus on 2020/3/9.
//
//
#import "PhobosSendDataEntity+CoreDataClass.h"
@implementation PhobosSendDataEntity
@end
//
// PhobosSendDataEntity+CoreDataProperties.h
// GMPhobos
//
// Created by Locus on 2020/3/9.
//
//
#import "PhobosSendDataEntity+CoreDataClass.h"
NS_ASSUME_NONNULL_BEGIN
@interface PhobosSendDataEntity (CoreDataProperties)
+ (NSFetchRequest<PhobosSendDataEntity *> *)fetchRequest;
@property (nonatomic) int64_t id;
@property (nullable, nonatomic, retain) NSData *data;
@property (nullable, nonatomic, copy) NSString *api;
@property (nonatomic) int16_t status;
@end
NS_ASSUME_NONNULL_END
//
// PhobosSendDataEntity+CoreDataProperties.m
// GMPhobos
//
// Created by Locus on 2020/3/9.
//
//
#import "PhobosSendDataEntity+CoreDataProperties.h"
@implementation PhobosSendDataEntity (CoreDataProperties)
+ (NSFetchRequest<PhobosSendDataEntity *> *)fetchRequest {
return [NSFetchRequest fetchRequestWithEntityName:@"PhobosSendDataEntity"];
}
@dynamic id;
@dynamic data;
@dynamic api;
@dynamic status;
@end
......@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface PhobosSendManager : NSObject
+ (void)sendDataWithEntities:(NSArray<PhobosDataEntity *> *)needSendData completion:(nullable void(^)(NSArray<PhobosDataEntity *> *finishEntities, NSInteger code))completion;
+ (void)sendDataWithEntities:(NSArray<PhobosSendDataEntity *> *)needSendData completion:(nullable void(^)(NSArray<PhobosSendDataEntity *> *finishEntities, NSInteger code))completion;
@end
......
......@@ -53,21 +53,21 @@
}
}
+ (void)sendDataWithEntities:(NSArray<PhobosDataEntity *> *)sendDataEntities completion:(void (^)(NSArray<PhobosDataEntity *> * _Nonnull, NSInteger))completion {
+ (void)sendDataWithEntities:(NSArray<PhobosSendDataEntity *> *)sendDataEntities completion:(void (^)(NSArray<PhobosSendDataEntity *> * _Nonnull, NSInteger))completion {
if (sendDataEntities.count == 0) {
return;
}
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
[sendDataEntities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDataEntities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *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<PhobosDataEntity *> *entities, BOOL * _Nonnull stop) {
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosSendDataEntity *> *entities, BOOL * _Nonnull stop) {
// TODO: 如果数据量过大,
NSMutableArray *sendDatas = [NSMutableArray new];
[entities enumerateObjectsUsingBlock:^(PhobosDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[sendDatas addObject:[obj.data mj_JSONObject]];
}];
......
<?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="PhobosSendDataEntity" representedClassName="PhobosSendDataEntity" syncable="YES">
<attribute name="api" optional="YES" attributeType="String"/>
<attribute name="data" optional="YES" attributeType="Binary"/>
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="status" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
</entity>
<elements>
<element name="PhobosSendDataEntity" positionX="-54" positionY="-9" width="128" height="103"/>
</elements>
</model>
\ No newline at end of file
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