Easily integrate your Realm models with a JSON:API compliant server
-
Define a Realm model
-
Define
JSONtoModelMap
(and we strongly recommenddefaultAttributes
anddefaultRelationships
as well) -
Register the model early in the application's lifecycle via
[[JSONAPIResourceRegistry sharedInstance] bindJSONType:@"your-model-type" toClass:[Model class]]
-
Parse server responses with
[JSONAPIParserUtilities putJSON:serverResponseDict inRealm:[RLMRealm defaultRealm]]
-
Serialize your model to JSON with
[model toJSON]
(made accessible via#import <Realm_JSONAPI/RLMObject+JSONAPI.h>
) and send it to the server
Try out a full example project with
pod try Realm-JSONAPI
Typical usage in a single RLMObject subclass file looks something like this:
#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];
}
You can read the full docs at http://cocoadocs.org/docsets/Realm-JSONAPI
Realm and a JSON:API-compliant server
Realm-JSONAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Realm-JSONAPI"
David Kettler, david@patreon.com
Realm-JSONAPI is available under the Apache 2.0 license. See the LICENSE file for more info.
git clone git@github.com:Patreon/Realm-JSONAPI.git
cd Realm-JSONAPI
git checkout -b my-meaningful-improvements
- Write beautiful code that improves the project, creating or modifying tests to prove correctness.
- Commit said code and tests in a well-organized way.
- Confirm tests pass by opening
Example/Realm-JSONAPI.xcworkspace
and running tests withCmd+U
(you may need tocd Example && pod install
first) git push origin my-meaningful-improvements
- Open a pull request (
hub pull-request
, if you havehub
) - Have a chill discussion with the community about how to best integrate your improvements into mainline deployments