Realm-JSONAPI 0.1.4

Realm-JSONAPI 0.1.4

测试已测试
语言语言 Obj-CObjective C
许可证 Apache 2
发布最新发布2016年11月

David KettlerMayuko InoueSeansy Holbert 维护。



  • 作者:
  • David Kettler

CI Status

轻松将您的 Realm 模型与符合 JSON:API 的服务器集成

目录

  1. 用法
  2. 示例
  3. 文档
  4. 要求
  5. 安装
  6. 作者
  7. 许可证
  8. 贡献

用法

  1. 定义一个 Realm 模型
  2. 定义 JSONtoModelMap(此外,我们强烈推荐使用 defaultAttributesdefaultRelationships
  3. 通过以下方式在应用的生命周期早期注册模型:

    [[JSONAPIResourceRegistry sharedInstance] bindJSONType:@"your-model-type" toClass:[Model class]]

  4. 使用以下方式解析服务器响应:

    [JSONAPIParserUtilities putJSON:serverResponseDict inRealm:[RLMRealm defaultRealm]]

  5. 使用 [model toJSON] 将您的模型序列化为 JSON(通过 #import <Realm_JSONAPI/RLMObject+JSONAPI.h> 使其可用)并发送到服务器

示例

通过以下方式尝试一个完整的示例项目:

pod try Realm-JSONAPI

在单个 RLMObject 子类文件中的典型使用看起来像这样:

#import <Realm_JSONAPI/RLMObject+JSONAPI.h>

@interface User : RLMModel
@property NSString *uid;
@property NSString *name;
@property NSString *email;
@property NSString *avatarURL;
@end

@implementation User

+ (NSDictionary *)JSONtoModelMap {
    return @{
        @"id" : @"uid",
        @"full_name" : @"fullName",
        @"email" : @"email",
        @"image_url": @"avatarURL",
    };
}

+ (NSArray *)defaultRelationships {
    return @[];
}

+ (NSArray *)defaultAttributes {
    return @[
        @"full_name",
        @"email",
        @"image_url",
    ];
}

+ (NSString *)primaryKey {
    return @"uid";
}

+ (void)fetchUser:(NSString *)uid {
    NSString *baseURL = [NSString stringWithFormat:@"users/%@", uid];
    [APICall queueWithURL:[[self class] defaultURLDecoration:baseURL]
                   params:nil
                   method:HttpMethodGET
              andCallback:callback];
}

- (void)patchWithCallback:(APICompletionBlock)callback {
    NSString *baseURL = [NSString stringWithFormat:@"users/%@", self.uid];
    [APICall queueWithURL:[[self class] defaultURLDecoration:baseURL]
                   params:[self toJSON]
                   method:HttpMethodPATCH
              andCallback:callback];
}

文档

您可以在 http://cocoadocs.org/docsets/Realm-JSONAPI 上阅读完整的文档

要求

Realm 和一个符合 JSON:API 的服务器

安装

Realm-JSONAPI 通过 CocoaPods 提供。要安装它,请在您的 Podfile 中添加以下行

pod "Realm-JSONAPI"

作者

David Kettler,[email protected]

许可证

Realm-JSONAPI 在 Apache 2.0 许可证下提供。有关更多信息,请参阅 LICENSE 文件

贡献

  1. git clone [email protected]:Patreon/Realm-JSONAPI.git
  2. cd Realm-JSONAPI
  3. git checkout -b my-meaningful-improvements
  4. 编写改进项目的漂亮代码,创建或修改测试来证明正确性。
  5. 以整洁的方式提交相关代码和测试。
  6. 通过打开 Example/Realm-JSONAPI.xcworkspace 并使用 Cmd+U 运行测试来确认测试通过(可能需要先执行 cd Example && pod install
  7. 推送代码至 origin 上的 my-meaningful-improvements
  8. 提交一个 pull request(《hub pull-request》,如果已安装 hub
  9. 与社区进行轻松讨论,了解如何最好地将您的改进集成到主线上