gpt4 book ai didi

restkit - 我可以使用 RestKit 和 Realm.io 吗?

转载 作者:行者123 更新时间:2023-12-04 08:43:42 27 4
gpt4 key购买 nike

我想用RestKit ,但我已经使用 Realm.io而不是 CoreData。

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

最佳答案

你当然可以。从 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];

你需要做两件事:
  • 创建继承自 RLMObject
  • 的 RealmArticle 模型(在本例中)
  • 然后你只需要写到你的 Realm
    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];

    [RealmArticle createInDefaultRealmWithObject:article];

    [realm commitWriteTransaction];
  • 关于restkit - 我可以使用 RestKit 和 Realm.io 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26252759/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com