同步您的对象图数据模型与它的云端后端。CloudBridge 支持与 RESTful 后端结合的 CoreData、Realm 和普通的 JSON 模型。它是基于 AFNetworking 开发的。
安装
CloudBridge 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "CloudBridge"
入门
初始化堆栈
// 1. Initializing a core data stack
NSURL *momURL = [NSBundle.mainBundle URLForResource:@"MyModel" withExtension:@"mom"];
NSURL *libraryDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask].lastObject;
NSURL *location = [libraryDirectory URLByAppendingPathComponent:@"store.sqlite"];
CBRCoreDataStack *coreDataStack = [[CBRCoreDataStack alloc] initWithType:NSSQLiteStoreType location:location model:momURL inBundle:NSBundle.mainBundle type:CBRCoreDataStackTypeParallel];
// 2. Defining the data interface
CBRCoreDataInterface *coreDataInterface = [[CBRCoreDataInterface alloc] initWithStack:[CBRCoreDataStack iCuisineStack]];
// 3. Defining a property mapping
CBRUnderscoredPropertyMapping *propertyMapping = [[CBRUnderscoredPropertyMapping alloc] init];
[propertyMapping registerObjcNamingConvention:@"uuid" forJSONNamingConvention:@"UUID"];
[propertyMapping registerObjcNamingConvention:@"identifier" forJSONNamingConvention:@"id"];
[propertyMapping registerObjcNamingConvention:@"identifiers" forJSONNamingConvention:@"ids"];
// 4. Defining the network connection
AFHTTPSessionManager *sessionManager = ...
CBRRESTConnection *connection = [[CBRRESTConnection alloc] initWithPropertyMapping:propertyMapping sessionManager:sessionManager];
connection.objectTransformer.dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
connection.objectTransformer.dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
// 5. Creating the cloud bridge
CBRThreadingEnvironment *threadingEnvironment = [[CBRThreadingEnvironment alloc] initWithCoreDataAdapter:coreDataInterface];
CBRCloudBridge *cloudBridge = [[CBRCloudBridge alloc] initWithCloudConnection:connection interface:coreDataInterface threadingEnvironment:threadingEnvironment];
// 6. Initialising the data model
NSManagedObject.cloudBridge = cloudBridge;
传输数据
CloudBridge 在数据模型上暴露了一系列方便的方法来与云端后端交互
+ (void)fetchObjectFromPath:(NSString *)path withCompletionHandler:(void (^)(id fetchedObject, NSError *error))completionHandler; // GET path
+ (void)fetchObjectsFromPath:(NSString *)path withCompletionHandler:(void (^)(NSArray *fetchedObjects, NSError *error))completionHandler; // GET path
- (void)createToPath:(NSString *)path withCompletionHandler:(void(^)(id fetchedObject, NSError *error))completionHandler; // POST path
- (void)reloadFromPath:(NSString *)path withCompletionHandler:(void(^)(id fetchedObject, NSError *error))completionHandler; // GET path
- (void)saveToPath:(NSString *)path withCompletionHandler:(void(^)(id fetchedObject, NSError *error))completionHandler; // PUT path
- (void)deleteToPath:(NSString *)path withCompletionHandler:(void(^)(NSError *error))completionHandler; // DELETE path
并支持对象图
- (void)fetchObjectForRelationship:(NSString *)relationship withCompletionHandler:(void(^)(idobject, NSError *error))completionHandler;
- (void)fetchObjectsForRelationship:(NSString *)relationship withCompletionHandler:(void(^)(NSArray *objects, NSError *error))completionHandler;
如果路径映射存储在模型的用户信息字典中,则还有更多方便的方法可用
- (void)createWithCompletionHandler:(void(^_Nullable)(id fetchedObject, NSError *error))completionHandler;
- (void)reloadWithCompletionHandler:(void(^)(id fetchedObject, NSError *error))completionHandler;
- (void)saveWithCompletionHandler:(void(^)(id fetchedObject, NSError *error))completionHandler;
- (void)deleteWithCompletionHandler:(void(^)(NSError *error))completionHandler;