我可以使用RestKit和Realm.io吗? [英] Can I use RestKit and Realm.io?

查看:65
本文介绍了我可以使用RestKit和Realm.io吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RestKit ,但是我已经在使用

I want to use RestKit, but I already use Realm.io instead of CoreData.

是否可以在Realm.io上使用RestKit?

Is it possible to use RestKit on top of Realm.io?

推荐答案

可以.从RestKit取回对象后:

Sure you can. Once you get the object back from RestKit:

// GET a single Article from /articles/1234.json and map it into an object
// JSON looks like {"article": {"title": "My Article", "author": "Blake", "body": "Very cool!!"}}
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Article class]];
[mapping addAttributeMappingsFromArray:@[@"title", @"author", @"body"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:@"/articles/:articleID" keyPath:@"article" statusCodes:statusCodes];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://restkit.org/articles/1234.json"]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    Article *article = [result firstObject];


    // I would put the Realm write here


    NSLog(@"Mapped the article: %@", article);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
[operation start];

您将需要做两件事:

  1. 创建从RLMObject继承的RealmArticle模型(在本例中)
  2. 然后,您只需要写到您的领域

  1. Create your RealmArticle model (in this case) that inherits from RLMObject
  2. Then you will just need to write to your realm

RLMRealm *realm = [RLMRealm defaultRealm];

[realm beginWriteTransaction];

[RealmArticle createInDefaultRealmWithObject:article];

[realm commitWriteTransaction];

这篇关于我可以使用RestKit和Realm.io吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆