2014-10-08 27 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]; 

获取对象后面你需要做两件事情:

  1. 创建从RLMObject
  2. 继承
  3. 那么你RealmArticle模型(在这种情况下),你只需要写入您的领域

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