2014-10-08 4 views

ответ

3

уверенный вы можете. После того, как вы получите объект обратно из 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]; 

Вам нужно будет сделать 2 вещи:

  1. Создайте модель RealmArticle (в данном случае), который наследуется от RLMObject
  2. Тогда вы будете просто необходимо указать в вашем регионе

    RLMRealm *realm = [RLMRealm defaultRealm]; 
    
    [realm beginWriteTransaction]; 
    
    [RealmArticle createInDefaultRealmWithObject:article]; 
    
    [realm commitWriteTransaction];