使用RestKit同时处理瞬态和持久对象(核心数据)

时间:2014-01-29 11:27:38

标签: ios core-data restkit restkit-0.20

我正在使用RestKit将数据存储在Core Data中我已经将它们存储在瞬态对象上。如何在Core Data中平行保存它们。

我应该将PersistentStore分配给RKObjectManager还是NSManagedObjectContext分配给RKObjectManager

我经历了很多教程,每个教程都与其他教程发生冲突。 建议一些明确的教程。

在浏览github教程后,我编写了以下代码,用于将数据映射到coredata和瞬态对象。但它无法正常工作

//分配模型

     NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]         pathForResource:@"Sample" ofType:@"momd"]];
     NSLog(@"sql:%@",[RKApplicationDataDirectory() stringByAppendingPathComponent:@"Sample![enter image description here][1].sqlite"]);
    objectManager=[[RKObjectManager alloc]init];

  // NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
     NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
     managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

     objectManager.managedObjectStore = managedObjectStore;

     NSError *error;
     [managedObjectStore addSQLitePersistentStoreAtPath:[RKApplicationDataDirectory()   stringByAppendingPathComponent:@"FourthEstateApp.sqlite"] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
     NSLog(@"sql:%@",[RKApplicationDataDirectory() stringByAppendingPathComponent:@"FourthEstateApp.sqlite"]);
     [managedObjectStore createManagedObjectContexts];

//创建映射

      storyDataMapping=[RKEntityMapping mappingForEntityForName:@"Story" inManagedObjectStore:managedObjectStore];
      [storyDataMapping addAttributeMappingsFromArray:[NSArray arrayWithObjects:
                                             @"shares",
                                             @"views",
                                             @"weight",
                                             @"story_url",
                                             @"other_articles",
                                             @"color",
                                             @"public_url",
                                             @"images_urls", nil
                                             ]
 ];

    articleDataMapping=[RKEntityMapping mappingForEntityForName:@"Article"   inManagedObjectStore:managedObjectStore];
    [articleDataMapping addAttributeMappingsFromArray:[
                                               NSArray arrayWithObjects:@"location", @"original_url",
                                               @"publication_date_time", @"title",
                                               @"article_url", @"source_name",
                                               @"source_twitter_id", nil
                                               ]
 ];

[storyDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"main_article"
                                                                             t    toKeyPath:@"main_article"
                                                                           withMapping:articleDataMapping]];
responseDescriptorForStoryData = [RKResponseDescriptor responseDescriptorWithMapping:storyDataMapping method:RKRequestMethodGET
                                                                        pathPattern:Nil keyPath:nil statusCodes:
                                 RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptorsFromArray:@[responseDescriptorForStoryData]];

    NSURL *URL = [NSURL URLWithString:categoryURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

   RKManagedObjectRequestOperation *objectRequestOperation =  [[RKManagedObjectRequestOperation alloc] initWithRequest:request
                                                                               responseDescriptors:@[responseDescriptorForStoryData]];
     [objectRequestOperation
     setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
     NSLog(@"Load collection of Articles: %@", mappingResult);

    for (Story *s in mappingResult.array) {
      NSLog(@"storymain article is:%@", s.main_article.title);
      [stories addObject:s];
     } 

//成功从coredata获取数据

     NSManagedObjectContext *managedObjectContext =[[NSManagedObjectContext alloc]init];
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Story" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];

      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      [request setEntity:entity];


      // Execute the fetch -- create a mutable copy of the result.
      NSError *error = nil;
      NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
      NSLog(@"results from cd:%@ error:%@",mutableFetchResults,error);

    [[NSNotificationCenter defaultCenter] postNotificationName:@"success" object:stories];
  }

                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          //RKLogError(@"Operation failed with error: %@", error);
                          [[NSNotificationCenter defaultCenter] postNotificationName:@"failure" object:error];
                          NSLog(@"Operation failed with error: %@", error);
                        }
     ];

在此输入代码

   [objectManager enqueueObjectRequestOperation:objectRequestOperation];
   [objectRequestOperation start];

执行successblock 当我试图在发生错误后获取数据时

  

CoreData:错误:无法在NSManagedObject类'Story'

上调用指定的初始值设定项

0 个答案:

没有答案