2011-12-04 17 views
6

मैं अपने टेबलव्यू के साथ काम कर रहे NSFetchedResultsController प्राप्त करने का प्रयास कर रहा हूं, लेकिन इसे सही तरीके से सेटअप करने के अपने सर्वोत्तम प्रयासों के बावजूद, यह हमेशा कोई पंक्ति नहीं लौटा रहा है। मैंने फाइंडर के माध्यम से अपना डेटा स्टोर खोला है और एक SQLite संपादक के माध्यम से सत्यापित किया है कि वास्तव में बहुत सारे रिकॉर्ड हैं, लेकिन यह हमेशा शून्य लौटाता है। मैं क्या खो रहा हूँ?NSFetchedResultsController हमेशा कोई पंक्ति नहीं देता

नियंत्रक के लिए कस्टम गेटर:

- (NSFetchedResultsController *)fetchedResultsController { 
    if (fetchedResultsController_ != nil) { 
     return fetchedResultsController_; 
    } 

    RBGameItemController* itemController = [RBGameItemController sharedInstance]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GameItem" inManagedObjectContext:itemController.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                            managedObjectContext:itemController.managedObjectContext 
                            sectionNameKeyPath:nil 
                              cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    self.fetchedResultsController.delegate = self; 

    [sort release]; 
    [fetchRequest release]; 
    [theFetchedResultsController release]; 

    return self.fetchedResultsController; 
} 

मैं डेटा प्राप्त करने में कर रहा हूँ viewDidLoad में:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    } 
} 

लाए गए परिणाम प्रतिनिधि:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 

    NSLog(@"controllerWillChangeContent"); 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    NSLog(@"controller:didChangeObject:"); 
    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      // Reloading the section inserts a new row and ensures that titles are updated appropriately. 
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
    NSLog(@"controller:didChangeSection:"); 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    NSLog(@"controllerDidChangeContent:"); 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 

मैं अनुभाग के लिए दोनों तरीकों को लागू करने कर रहा हूँ और पंक्ति संख्या, जो पंक्तियों के लिए खंडों और 0 के लिए 1 लौटाती है:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
    int sections = [[self.fetchedResultsController sections] count]; 
    NSLog(@"sections=%i", sections); 
    return sections; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    int rows = [sectionInfo numberOfObjects]; 

    NSLog(@"rows=%i", rows); 

    return rows; 
} 

उत्तर

11

क्या आपने यह सुनिश्चित करने के लिए मैन्युअल रूप से एक प्रश्न निष्पादित करने का प्रयास किया है कि आप क्या उम्मीद करते हैं? अपने कस्टम में करने का प्रयास करें fetchedResultsController:

NSArray *entities = [itemController.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
NSLog(@"%d",entities.count); 

और देखते हैं कि वापस आता है।

इसके अलावा, कैश के बिना अपना fetchedRequestController सेट अप करने का प्रयास करें। हो सकता है कि आप एप्लिकेशन के अन्य हिस्सों में नाम से उसी कैश का पुन: उपयोग कर रहे हों?

+1

तो मैं एक लाने किया यह पंक्तियाँ मैं उम्मीद कर रहा था लौटे:

static NSString *const NSString *kMyFetchedResultsControllerCacheName = @"RootCache"; - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController == nil) { [NSFetchedResultsController deleteCacheWithName:]; RBGameItemController* itemController = [RBGameItemController sharedInstance]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([GameItem class]) inManagedObjectContext:itemController.managedObjectContext]; [fetchRequest setEntity:entity]; NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; [fetchRequest setFetchBatchSize:20]; NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:itemController.managedObjectContext sectionNameKeyPath:nil cacheName:kMyFetchedResultsControllerCacheName]; self.fetchedResultsController = fetchedResultsController; fetchedResultsController.delegate = self; } return _fetchedResultsController; } 

अंत में, आप हमेशा यह सुनिश्चित performFetch कहा जाता है, शायद viewDidLoad में बनाने की जरूरत है। मैं क्या कर रहा था कैशनाम के लिए 'शून्य' में गुजर रहा था। यह कोई मुद्दा नहीं होना चाहिए था, क्योंकि यह एकमात्र ऐसा स्थान है जहां मेरे पास वर्तमान में 'NSFetchedResultsController' है, इसलिए मुझे परेशान है कि कोई समस्या क्यों थी। –

+0

@WayneHartman कैश नाम के लिए नील पास करने के लिए भी मेरे लिए इस मुद्दे को हल किया। मैं ऐप में कहीं और कैश का उपयोग नहीं करता, इसलिए मैं इस बारे में उलझन में हूं कि इसे निर्दिष्ट करने से मुझे शून्य परिणाम क्यों दिए गए। – deepwinter

0

कैश के लिए शून्य गुजरने के लिए नाम ने मेरे लिए भी इस मुद्दे को हल किया ... अभी तक सुनिश्चित नहीं है।

5

प्रतिनिधि के लिए काम:

self.fetchedResultsController.delegate = self; 

अंतिम पंक्ति:

return self.fetchedResultsController; 

ये इस विधि के लिए एक परिपत्र कॉल का परिणाम देगा। इस तरह कोड लिखो मत।

इस कॉल में आलोचनात्मक अंक प्रबंधित ऑब्जेक्ट कॉन्टेक्स्ट, सॉर्ट डिस्क्रिप्टर और इकाई हैं। तो सुनिश्चित करें कि इनमें से कोई भी शून्य नहीं है और वह मान हैं जो आप उम्मीद करते हैं। और

NSError *error = nil; 
[self.fetchedResultsController performFetch:&error]; 
if (error != nil) { 
    NSLog(@"%@", error.localizedDescription); 
} 
+1

मैं निश्चित रूप से ऐसा करने के लिए अपने डेढ़ साल पहले स्वयं को थप्पड़ मार दूंगा। ;) –

संबंधित मुद्दे