测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | Apache 2 |
发布最新发布 | 2016年11月 |
由 David Kettler,Mayuko Inoue,Seansy Holbert 维护。
轻松将您的 Realm 模型与符合 JSON:API 的服务器集成
JSONtoModelMap
(此外,我们强烈推荐使用 defaultAttributes
和 defaultRelationships
)通过以下方式在应用的生命周期早期注册模型:
[[JSONAPIResourceRegistry sharedInstance] bindJSONType:@"your-model-type" toClass:[Model class]]
使用以下方式解析服务器响应:
[JSONAPIParserUtilities putJSON:serverResponseDict inRealm:[RLMRealm defaultRealm]]
[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-JSONAPI 通过 CocoaPods 提供。要安装它,请在您的 Podfile 中添加以下行
pod "Realm-JSONAPI"
David Kettler,[email protected]
Realm-JSONAPI 在 Apache 2.0 许可证下提供。有关更多信息,请参阅 LICENSE 文件
git clone [email protected]:Patreon/Realm-JSONAPI.git
cd Realm-JSONAPI
git checkout -b my-meaningful-improvements
Example/Realm-JSONAPI.xcworkspace
并使用 Cmd+U
运行测试来确认测试通过(可能需要先执行 cd Example && pod install
)推送代码至 origin 上的 my-meaningful-improvements
hub
)