मेरे पास एक सेक्शन के साथ एक परिणाम प्राप्त हुआ है। मैं सभी ऑब्जेक्ट्स के लिए [[fetchedResultsController fetchedObjects] objectAtIndex:index]
का उपयोग कर ऑब्जेक्ट तक पहुंचने में सक्षम हूं। लेकिन जब मैं ऑब्जेक्ट एट इंडेक्सपैथ का उपयोग करता हूं तो यह असफल हो रहा है: [fetchedResultsController objectAtIndexpath:indexPath]
NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject असंगतता
एक पंक्ति (सर्च रेसल्ट ऑब्जेक्ट्स में से एक के लिए) को संबंधित तालिका में डालने के बाद एक त्रुटि होती है। ऑब्जेक्ट को नई तालिका में सही ढंग से डाला जा रहा प्रतीत होता है। मैंने दृष्टि से इसकी पुष्टि करने के बाद, मैं पंक्तियों में से एक का चयन करता हूं, और फिर मजा शुरू होता है।
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SearchResult *event;
NSLog(@"Number of sections in fetchedResultsController: %d", [[fetchedResultsController sections] count]);
for (NSUInteger i=0; i<[[fetchedResultsController fetchedObjects] count]; i++) {
event = (SearchResult*)[[fetchedResultsController fetchedObjects] objectAtIndex:i];
NSLog(@"object at index[%d]: %@", i, event.title);
NSLog(@"indexPath for object at index[%d]:%@", i, [fetchedResultsController indexPathForObject:event]);
}
NSLog(@"indexPath passed to method: %@", indexPath);
SearchResult *result = [fetchedResultsController objectAtIndexPath:indexPath]; // *** here is where the error occurs ***
[viewDelegate getDetails:result];
}
मैं अंतिम पंक्ति में एक विफलता कर रहा हूँ:
यहाँ कोड जहां त्रुटि उत्पन्न हो रही है है। लॉग इस तरह दिखता है:
Number of sections in fetchedResultsController: 1
object at index[0]: Cirles
indexPath for object at index[0]:(null)
object at index[1]: Begin
indexPath for object at index[1]:(null)
object at index[2]: Copy
indexPath for object at index[2]:(null)
object at index[3]: Unbound
indexPath for object at index[3]:(null)
indexPath passed to method: <NSIndexPath 0x64ddea0> 2 indexes [0, 2]
NSLog बयान को क्रियान्वित करने के बाद, मैं [fetchedResultsController objectAtIndexPath:indexPath]
का उपयोग कर अंतिम पंक्ति में एक अपवाद मिलता है। यह अन्य सूचकांक मूल्यों के लिए भी होता है, लेकिन वे हमेशा वैध दिखते हैं।
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'
तो, संक्षेप में प्रस्तुत करने, वहाँ प्राप्त किए गए वस्तुओं की सही संख्या दिखाई देते हैं, वहाँ एक अनुभाग (0), मैं एक के बाद एक विधि से प्रत्येक का उपयोग कर सकते हैं, लेकिन अन्य द्वारा नहीं है। IndexPathForObject: हमेशा लौट रहा है (शून्य)।
क्या यह एक बग है या क्या मैं कुछ गलत समझ रहा हूं?
अद्यतन
यहाँ कोड, NSFetchedResultsControllerDelegate प्रोटोकॉल तरीकों को लागू करने है।
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"Favorites controllerWillChangeContent");
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"Favorites Table controllerDidChangeContent");
[self.tableView endUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
NSLog(@"Favorites Changed Object");
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"--- Favorite was inserted");
if ([[self.fetchedResultsController fetchedObjects] count] == 1) {
// configure first cell to replace the empty list indicator by first deleting the "empty" row
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeDelete:
NSLog(@"--- Favorite was deleted");
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
if ([[self.fetchedResultsController fetchedObjects] count] == 0) {
// configure first cell to show that we have an empty list
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
break;
case NSFetchedResultsChangeMove:
NSLog(@"--- Favorite was moved");
break;
case NSFetchedResultsChangeUpdate:
NSLog(@"--- Favorite was updated");
[self configureCell:(ResultCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
default:
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type {
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"Favorites Section Added");
break;
case NSFetchedResultsChangeDelete:
NSLog(@"Favorites Section Deleted");
break;
default:
break;
}
}
अद्यतन 2
अगर यह मायने रखती है मैं नहीं जानता, लेकिन tableView इस लाइन के साथ आरंभ नहीं हो जाता:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped];
अद्यतन 3
जब मैं अपमानजनक रेखा को निम्नानुसार बदलता हूं, तो यह काम करता है ठीक है लेकिन मैं इंडेक्सिंग को टेबल के साथ ही रखना चाहूंगा।
//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath];
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]]
आपने ऑब्जेक्ट कैसे डाला? क्या आप उस कोड को पोस्ट कर सकते हैं जहां आप ऑब्जेक्ट को अपने मूल डेटा स्टोर में जोड़ रहे हैं और अपनी तालिका में पंक्ति जोड़ सकते हैं? – timthetoolman
अपडेट किया गया। ऊपर देखो। – Jim
क्या आप वास्तव में कोर डेटा स्टोर में कुछ जोड़ रहे हैं या बस तालिका में एक पंक्ति जोड़ रहे हैं? – timthetoolman