Code Monkey home page Code Monkey logo

afcoredata's Introduction

AFCoreData

使用 AFNetworking2.0 集成了coredata 做本地缓存

这个小 demo 改自afnetworing 提供的 AFIncrementalStore 使用最新版本的AFNetworking

  1. 使用该项目提供的 AFCoreData 需要重写 AppDelegate 下 的 - (NSManagedObjectContext *)managedObjectContext ,- (NSPersistentStoreCoordinator *)persistentStoreCoordinato 两个方法

demo

  • (NSPersistentStoreCoordinator *)persistentStoreCoordinator { // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; }

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    AFIncrementalStore *incrementalStore = (AFIncrementalStore *)[_persistentStoreCoordinator addPersistentStoreWithType:[AppDotNetIncrementalStore type] configuration:nil URL:nil options:nil error:nil]; NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataTest.sqlite"];

    NSDictionary *options = @{ NSInferMappingModelAutomaticallyOption : @(YES), NSMigratePersistentStoresAutomaticallyOption: @(YES) }; NSError *error = nil;

    NSString *failureReason = @"There was an error creating or loading the application's saved data."; if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); }

    return _persistentStoreCoordinator; }

  • (NSManagedObjectContext *)managedObjectContext { // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) if (_managedObjectContext != nil) { return _managedObjectContext; }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (!coordinator) { return nil; } _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:coordinator];

    NSLog(@"managedObjectContext success %@", _managedObjectContext);

    return _managedObjectContext; }

  1. 继承 AFIncrementalStore 重写 + (NSString *)type ,+ (NSManagedObjectModel *)model 两个方法

示例代码见本工程

  1. 继承 AFRESTClient 自定义 inflector ,paginator 或者重写 #pragma mark Read Methods 下的方法

详细见工程

afcoredata's People

Contributors

juvham avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.