设为首页 加入收藏

TOP

Core Data with Mantle(一)
2015-07-24 06:11:32 来源: 作者: 【 】 浏览:27
Tags:Core Data with Mantle

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?在CODE上查看代码片派生到我的代码片
        1. #import
        2. @interface BannerWrapper : MTLModel
        3. @property (nonatomic, readonly) bool result;
        4. @property (copy, nonatomic, readonly) NSNumber *resultId;
        5. @property (copy, nonatomic, readonly) NSString *resultMsg;
        6. @property (copy, nonatomic) NSArray *bannerList;
        7. +(NSSortDescriptor *)sortDescriptor;
        8. @end
        9. @interface Banner : MTLModel
        10. @property (copy, nonatomic, readonly) NSNumber *bannerId;
        11. @property (copy, nonatomic, readonly) NSString *picUrl;
        12. @property (copy, nonatomic, readonly) NSString *toDetailUrl;
        13. @property (copy, nonatomic, readonly) NSNumber *width;
        14. @property (copy, nonatomic, readonly) NSNumber *height;
        15. @end

          .m文件

          [objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
          1. #import "Banner.h"
          2. @implementation BannerWrapper
          3. #pragma mark - JSON serialization
          4. + (NSDictionary *)JSONKeyPathsByPropertyKey {
          5. return @{
          6. @"result" : @"result",
          7. @"resultId" : @"resultId",
          8. @"resultMsg" : @"resultMSG",
          9. @"bannerList" : @"banner"
          10. };
          11. }
          12. + (NSValueTransformer *)bannerListJSONTransformer
          13. {
          14. return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:[Banner class]];
          15. }
          16. #pragma mark - Managed object serialization
          17. + (NSString *)managedObjectEntityName {
          18. return @"BannerWrapper";
          19. }
          20. + (NSDictionary *)managedObjectKeysByPropertyKey {
          21. return nil;
          22. }
          23. +(NSDictionary *)relationshipModelClassesByPropertyKey{
          24. return @{
          25. @"bannerList" : [Banner class],
          26. };
          27. }
          28. //在从coredata中取数据时的数据排序方式
          29. +(NSSortDescriptor *)sortDescriptor{
          30. return [[NSSortDescriptor alloc] initWithKey:@"resultId" ascending:YES];
          31. }
          32. @end
          33. @implementation Banner
          34. #pragma mark - JSON serialization
          35. + (NSDictionary *)JSONKeyPathsByPropertyKey {
          36. return @{
          37. @"bannerId" : @"id",
          38. @"picUrl" : @"picUrl",
          39. @"toDetailUrl" : @"toDetailUrl",
          40. @"width":@"width",
          41. @"height":@"height"
          42. };
          43. }
          44. #pragma mark - Managed object serialization
          45. + (NSString *)managedObjectEntityName {
          46. return @"Banner";
          47. }
          48. + (NSDictionary *)managedObjectKeysByPropertyKey {
          49. return nil;
          50. }
          51. @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 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇c++参数传递 下一篇uva 10006 数论入门题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: