Mantle makes it easy to write a simple model layer for your Cocoa or Cocoa Touch application. Mantle can still be a convenient translation layer between the API and your managed model objects.
本文使用mantle作data model,并使用其对coredata的interface创建数据持久化的过程。
操作过程很简单,就是数据的转换:

1.Manle data model
Mantle中用于持久化的方法:
// A MTLModel object that supports being serialized to and from Core Data as an
// NSManagedObject.
- @protocol MTLManagedObjectSerializing
- @required
// The name of the Core Data entity that the receiver serializes to and
// deserializes from.
- + (NSString *)managedObjectEntityName;
// Specifies how to map property keys to different keys on the receiver's
// +managedObjectEntity.
- + (NSDictionary *)managedObjectKeysByPropertyKey;
.h 文件
[objc] view plaincopyprint?
- #import
- @interface BannerWrapper : MTLModel
- @property (nonatomic, readonly) bool result;
- @property (copy, nonatomic, readonly) NSNumber *resultId;
- @property (copy, nonatomic, readonly) NSString *resultMsg;
- @property (copy, nonatomic) NSArray *bannerList;
- +(NSSortDescriptor *)sortDescriptor;
- @end
- @interface Banner : MTLModel
- @property (copy, nonatomic, readonly) NSNumber *bannerId;
- @property (copy, nonatomic, readonly) NSString *picUrl;
- @property (copy, nonatomic, readonly) NSString *toDetailUrl;
- @property (copy, nonatomic, readonly) NSNumber *width;
- @property (copy, nonatomic, readonly) NSNumber *height;
- @end
.m文件
[objc] view plaincopyprint?
- #import "Banner.h"
- @implementation BannerWrapper
- #pragma mark - JSON serialization
- + (NSDictionary *)JSONKeyPathsByPropertyKey {
- return @{
- @"result" : @"result",
- @"resultId" : @"resultId",
- @"resultMsg" : @"resultMSG",
- @"bannerList" : @"banner"
- };
- }
- + (NSValueTransformer *)bannerListJSONTransformer
- {
- return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:[Banner class]];
- }
- #pragma mark - Managed object serialization
- + (NSString *)managedObjectEntityName {
- return @"BannerWrapper";
- }
- + (NSDictionary *)managedObjectKeysByPropertyKey {
- return nil;
- }
- +(NSDictionary *)relationshipModelClassesByPropertyKey{
- return @{
- @"bannerList" : [Banner class],
- };
- }
- //在从coredata中取数据时的数据排序方式
- +(NSSortDescriptor *)sortDescriptor{
- return [[NSSortDescriptor alloc] initWithKey:@"resultId" ascending:YES];
- }
- @end
- @implementation Banner
- #pragma mark - JSON serialization
- + (NSDictionary *)JSONKeyPathsByPropertyKey {
- return @{
- @"bannerId" : @"id",
- @"picUrl" : @"picUrl",
- @"toDetailUrl" : @"toDetailUrl",
- @"width":@"width",
- @"height":@"height"
- };
- }
- #pragma mark - Managed object serialization
- + (NSString *)managedObjectEntityName {
- return @"Banner";
- }
- + (NSDictionary *)managedObjectKeysByPropertyKey {
- return nil;
- }
- @end
2.coredata 持久化类
Coredata主要元素简要介绍
网上有很多图,但,还是觉得一本书上的这个图最好:

- 1, Managed Object Model
Managed Object Model 是描述应用程序的数据模型,这个模型包含实体(Entity),特性(Property),读取请求(Fetch Request)等。(下文都使用英文术语。)
2,Managed Object Context
Managed Object Context 参与对数据对象进行各种操作的全过程,并监测数据对象的变化,以提供对 undo/redo 的支持及更新绑定到数据的 UI。
3,Persistent Store Coordinator
Persistent Store Coordinator 相当于数据文件管理器,处理底层的对数据文件的读取与写入。一般我们无需与它打交道。
4,Managed Object
Managed Object 数据对象,与 Managed Object Context 相关联。 -
5,Controller
图中绿色的 Array Controller, Object Controller, Tree Controller 这些控制器,一般都是通过 control+drag 将 Managed Object Context 绑定到它们,这样我们就可以
- 1, Managed Object Model
- #import
- + (NSDictionary *)managedObjectKeysByPropertyKey;
- + (NSString *)managedObjectEntityName;