11

के प्रतिनिधि से पकड़ा गया अपवाद मेरे एप्लिकेशन में दो टैब बार हैं ... प्रत्येक उपयोगकर्ता को टेबलव्यू कंट्रोलर पर ले जाता है जो उसे आइटमों की सूची के साथ प्रस्तुत करता है। पहला दृश्य उपयोगकर्ता को डेटाबेस में प्रविष्टियों को रिकॉर्ड करने देता है। अन्य टैब/व्यू डेटाबेस से पढ़ता है और उन आइटम्स को उपयोगकर्ता को भी प्रस्तुत करता है, हालांकि, इस दूसरे दृश्य से कोरडाटा/लगातार स्टोर में कोई अपडेट नहीं किया जाता है।CoreData त्रुटि मुझे पागल गाड़ी चला रहा है ... CoreData: गंभीर अनुप्रयोग त्रुटि। NSFetchedResultsController

जब मैं पहले व्यू कंट्रोलर के माध्यम से एक नया आइटम जोड़ता हूं, तो यह पूरी तरह से दृश्य में दिखाई देता है। हालांकि, जैसे ही मैं उस व्यू कंट्रोलर में नया आइटम दिखाई देने के लिए अन्य टैब बार पर टैप करता हूं, मुझे नीचे सूचीबद्ध त्रुटि मिलती है, और नया जोड़ा गया आइटम दिखाई नहीं देता है ... नोट: अगर मैं ऐप को रोकता हूं और पुनः लोड/इसे फिर से चलाएं, और दूसरी टैब-बार टैप करके शुरू करें, नया आइटम ठीक दिखता है, इसलिए मुझे पता है कि मॉडल को ठीक से अपडेट किया जा रहा है।

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 
2011-10-20 20:56:15.117 Gtrac[72773:fb03] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null) 

प्रतिनिधि आवेदन से कोड जहां प्रबंधित ऑब्जेक्ट कॉन्टेक्स्ट दो दृश्य नियंत्रकों को पास किया जाता है।

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 


    // get a point to the master database context 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (!context) { 
     // Handle the error. 
    } 

    // create tab bar controller and array to hold each of the tab-based view controllers that will appear as icons at bottom of screen 
    tabBarController = [[UITabBarController alloc] init]; 
    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:5];  


    // 
    // setup first tab bar item 
    // 
    // 
    // alloc the main view controller - the one that will be the first one shown in the navigation control 
    RootViewController *rootViewController = [[RootViewController alloc] initWithTabBar]; 

    // Pass the managed object context to the view controller. 
    rootViewController.managedObjectContext = context; 

    // create the navigation control and stuff the rootcontroller inside it 
    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 

    // set the master navigation control 
    self.navigationController = aNavigationController; 

    // add the navigaton controller as the first tab for the tab bar 
    [localControllersArray addObject:aNavigationController]; 

    [rootViewController release]; 
    [aNavigationController release];  


    // 
    // setup the other tab bar 
    // 
    // 

    // alloc the view controller 
    vcSimulator *vcSimulatorController = [[vcSimulator alloc] initWithTabBar]; 

    UINavigationController *blocalNavigationController = [[UINavigationController alloc] initWithRootViewController:vcSimulatorController]; 

    // Pass the managed object context to the view controller. 
    vcSimulatorController.managedObjectContext = context; 

    // add this controller to the array of controllers we are building 
    [localControllersArray addObject:blocalNavigationController]; 

    // release these guys, they are safely stored in the array - kill these extra references 
    [blocalNavigationController release]; 
    [vcSimulatorController release]; 


    // 
    // 
    // ok, all the tab bars are in the array - get crackin 
    // 
    // 
    // load up our tab bar controller with the view controllers 
    tabBarController.viewControllers = localControllersArray; 

    // release the array because the tab bar controller now has it 
    [localControllersArray release]; 

    [window addSubview:[tabBarController view]]; 
    [window makeKeyAndVisible]; 

    return YES; 




When I add a new item via the first viewcontroller, it shows up perfectly in the view. However, as soon as I tap on the other tab bar to see the new item appear in that viewcontroller, I get the error listed above, and the newly added item does not appear... Note: if I stop the app and reload/re-run it, and start by tapping the 2nd tabbar, the new item shows up fine, so I know the model is being updated fine. 

Here are the tableview delegate methods from the 2nd view controller. 



- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 

    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  
    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:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

      break; 
    } 

} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    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 { 
    [self.tableView endUpdates]; 

} 

कोई भी सहायता जो आप प्रदान कर सकते हैं उसकी बहुत सराहना की जाएगी।

मैंने इस साइट को खोजा है और इस त्रुटि के कई उदाहरण पाए हैं लेकिन कोई भी फिट नहीं है। मैं भी में निष्कर्ष निकालते हैं जो यह त्रुटि मैं दिखाई दे रही है वास्तव में एप्पल कोड में एक ज्ञात बग है संदर्भ देखा है ...

* अद्यतन जानकारी *

मैं वापस चला गया और निर्धारित किया है ब्रेक अंक कोड और इस अतिरिक्त जानकारी के साथ मूल प्रश्न को संपादित कर रहा हूं। जब उपयोगकर्ता डेटाबेस में एक नया आइटम जोड़ता है तो उसे रूटव्यू से सूची में बदल दिया जाता है। ऐड ट्रांजैक्शन बेकार ढंग से काम करता है और सूची कोर्स देखें UITableView पूरी तरह से अपडेट किया गया है।

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

सिम्युलेटर कुलपति:

- controllerWillChangeContent which runs... 
     [self.tableView beginUpdates]; 

    - didChangeObject 
     ..with message: NSFetchedResultsChangeUpdate 
     ..which ran: 
     [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 

    - controllerDidChangeContent: 
     [self.tableView endUpdates]; 

अन्य ViewController कि महान काम करता है, इस क्रम के तुरंत बाद रिकॉर्ड डेटाबेस में जोड़ा जाता है के माध्यम से चला जाता है।

ListCourses कुलपति:

- didChangeSection 
    ...with message: NSFetchedResultsChangeInsert 
...which ran: 
    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 

- didChangeObject 
    ..with message: NSFetchedResultsChangeInsert 
    ..which ran: 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

क्यों एक ViewController मिल NSFetchedResultsChangeInsert संदेश लेकिन एक दूसरे को नहीं करता है?

दोषपूर्ण व्यू कंट्रोलर से प्रतिनिधि विधियां यहां दी गई हैं।

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 





- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  


    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: 

      //[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      //[tableView reloadData]; 


      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 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; 
    } 
    NSLog(@"vc>>> about to reload data"); 
    // [self.tableView reloadData]; 

} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

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

} 


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

} 

धन्यवाद, फिल

उत्तर

20

UITableView विवेक इस तरह काम करता है की जाँच:

लाइन पर [self.tableView beginUpdates]; tableView आपकी तालिका को कॉल करता है दृश्य: संख्या OfRowsInSection: प्रतिनिधि विधि, जो लौटने लगती है 3. लाइन [self.tableView endUpdates] पर; यह इसे फिर से कॉल करता है और ऐसा लगता है कि यह 4 लौट रहा है।इसलिए, तालिका दृश्य आपको इन दो पंक्तियों के बीच 1 पंक्ति डालने की उम्मीद कर रहा है। वास्तव में, कोई भी पंक्तियां डाली नहीं जा रही हैं, इसलिए तालिका दृश्य एक दावा विफल रहता है। (आप दावा संदेश में अपेक्षित और वास्तविक पंक्ति गणना देख सकते हैं)।

3 पंक्तियों से 4 पंक्तियों में वृद्धि से पता चलता है कि आपका NSFetchedResultsController नए डाले गए कोर डेटा आइटम को सही ढंग से देख रहा है। आपको अपने नियंत्रक की शुरुआत में ब्रेकपॉइंट डालना है: didChangeObject: atIndexPath: forChangeType: विधि और चरण के माध्यम से जब आप किसी आइटम को डालने के बाद दूसरे टैब पर स्विच करते हैं। आपको NSFetchedResultsChangeInsert देखना चाहिए: स्विच स्टेटमेंट का केस किया जा रहा है लेकिन यह स्पष्ट रूप से नहीं हो रहा है।

उम्मीद है कि आप यह समझ सकते हैं कि सम्मिलन क्यों नहीं हो रहा है - अन्यथा वापस आएं और हमें बताएं कि इस विधि के माध्यम से कदम उठाने के दौरान आपने वास्तव में क्या देखा।

जोड़ने के लिए संपादित:

ठीक है, तो 2 दृश्य नियंत्रक में अपने NSFetchedResultsController प्रतिनिधि पद्धतियों को बुलाया जाता है जब आप तुरंत जब नया आइटम टैब 1. पर डाला जाता है बजाय कि टैब में स्विच यह है कि 2 का मतलब है देखें नियंत्रक डालने को नहीं देख रहा है (जो तुरंत होना चाहिए) और वास्तव में बाद में कुछ अन्य कोर डेटा अपडेट अधिसूचना का जवाब दे रहा है जो तब होता है जब आप टैब 2 पर स्विच करते हैं। प्राप्त परिणाम नियंत्रक स्टार्टअपडेट लाइन पर पुरानी जानकारी के साथ काम कर रहा है (वहां हैं परिणाम में वास्तव में 4 आइटम सेट नहीं 3)। जब तक यह अंत तक पहुंच जाता है तब तक लाइन ने इसे अपने रिफ्रेश को रीफ्रेश किया है और एक अप्रत्याशित डालने वाला पाया है।

NSFetchedResultsController प्रतिनिधि विधियां वास्तव में परिवर्तन करते समय UI को अद्यतन करने के लिए डिज़ाइन की गई हैं और नियंत्रक का दृश्य दृश्यमान है। आपके मामले में, आप परिवर्तन कर रहे हैं और फिर नया दृश्य नियंत्रक प्रदर्शित कर रहे हैं। पैटर्न है कि तुम सच में उपयोग करना चाहिए नियंत्रक के अपने viewWillAppear विधि में tableview ताज़ा करने के लिए है 2. इस तरह यह करना चाहिए:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    NSError *error = nil; 
    [resultsController performFetch:&error]; // Refetch data 
    if (error != nil) { 
     // handle error 
    } 

    [self.tableView reloadData]; 
} 

यह सुनिश्चित करें कि जब भी आप टैब 2 के लिए स्विच इसके साथ काम कर रहा है कर देगा मॉडल से ताजा डेटा।

+1

धन्यवाद, रॉबिन! मैं आपका अनुसरण करूंगा और आपको सुबह में बता दूंगा ... 1 बजे! – phil

+0

सब, कृपया मूल प्रश्न के अंत में अद्यतन जानकारी देखें। मैंने टेबलव्यू प्रतिनिधि विधियों और कॉल प्रवाह प्रदान किया है जो मैं ब्रेक पॉइंट्स और डीबगर के माध्यम से देख रहा हूं। कोई भी विचार क्यों मेरे वीसी सिम्युलेटर टेबलव्यू प्रतिनिधि विधियों को NSFetchedResultsChangeInsert संदेश कभी नहीं मिलता है? – phil

+0

उपरोक्त संपादित उत्तर। –

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