2013-02-21 64 views
0

我試圖從http json資源(類別列表)中獲取內容並將其與restkit和coredata連接起來。無法使Restkit 0.20 + CoreData工作

當我沒有使用CoreData時,我的映射用於工作。然後,我決定用下面的教程:

http://www.alexedge.co.uk/portfolio/introduction-restkit-0-20/

但是,我得到一個奇怪的錯誤,我只是不能找出原因:

the entity (null) is not key value coding-compliant for the key "remoteId" 

我的分類模型/實體具有映射remoteId到我的服務器上的ID,所以這不是問題。然而,從錯誤似乎Restkit或CoreData無法弄清楚,我說的是實體(他們說,這是一個空實體??)

這是請求代碼:

- (NSFetchedResultsController *)fetchedResultsController{ 
     if (!_fetchedResultsController) { 
      NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Category class])]; 
      fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]; 
      self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext sectionNameKeyPath:nil cacheName:@"Category"]; 
      self.fetchedResultsController.delegate = self; 
      NSError *error; 
      [self.fetchedResultsController performFetch:&error]; 
      NSLog(@"%@",[self.fetchedResultsController fetchedObjects]); 
      NSAssert(!error, @"Error performing fetch request: %@", error); 
     } 

     return _fetchedResultsController; 
    } 

而且映射:

+(void) prepareMapping { 
     RKObjectManager *manager = [RKObjectManager sharedManager]; 

     NSDictionary *categoryAttributes = @{ 
               @"id" : @"remoteId", 
               @"created_at" : @"updatedAt", 
               @"created_at" : @"createdAt", 
               @"name" : @"name", 
               @"ads_count": @"adsCount", 
              }; 

     RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Category" inManagedObjectStore:manager.managedObjectStore]; 

     [categoryMapping addAttributeMappingsFromDictionary:categoryAttributes]; 

     categoryMapping.identificationAttributes = @[@"remoteId"]; 

     [manager addResponseDescriptorsFromArray:@[ 
     [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping 
               pathPattern:@"neighborhoods/:neighborhoodId/categories.json" 
                keyPath:@"index_categories.index_category" 
               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)] 
     ]]; 
    } 

回答

1

我不知道是否會NSStringFromClass([Category class])工作。你有沒有嘗試下面的代碼?

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Category"]; 

此外,你可以檢查出的例子核心數據項目RestKit v.020-RC1 zip文件裏面看到的東西是怎麼回事。