更新18/3:我想我发现了问题,但我不确定我是否知道如何修复它.经过几个小时的实验,我发现当我在该会话期间在svc的主表视图中选择了多个项目时,我只能使应用程序崩溃.
当在主表视图中选择另一个项目时,详细信息表视图将获得一个新的对象集,并且如果它在更新/移动/删除/插入周期的中途,则称为刷新表.因此问题;我认为即使在detailvIEwcontroller上设置了新的故事对象,它也有更新旧故事对象的说明.
如何在详细视图中设置新故事之前完全处理动画/ coredata / tablevIEw更新?我在setEditting == NO时保存对managedobjectContext的更改.我猜我需要制作一个自定义的setStory setter,它在接受新对象之前处理UItableVIEw / CoreData集的所有更新?
在dIDSelectRowAtIndexPath中的svc的主tablevIEw控制器上调用此代码:
[detailVIEwController setStory:storySet]; //where storySet is the new story object[detailVIEwController refreshtables];
我尝试删除动作不会动画的行时出现间歇性错误,应用程序基本上会挂起以下错误(尽管从CoreData集中删除了行).如果我在一个会话中从svc中的主表视图控制器中选择了多行,则会发生这种情况.
在一些谷歌搜索后,我认为它可能是一个问题(voID)控制器:(NSFetchedResultsController *)控制器dIDChangeObject:(ID)在更新后调用,以动画用户所做的更改.
我该如何修复这些间歇性错误?
> 16/3我试图真正简化我的代码.我删除了对托管对象上下文的所有调用并将它们放入setEditing,删除了多余的[self.tablevIEw reloadData]和[self.tablevIEw setneedsdisplay]并完全使’重新排序’bool无效(它仍然在代码中,但它总是设置为NO,所以没有区别). UItableVIEw比以往任何时候都更加稳定,但我仍然设法在删除时出现间歇性错误(偶尔会添加) – 它似乎需要一段时间才能崩溃,但它仍然会.**
> 15/3:我认为它与CoreData / UItableVIEw不同步有关–CoreData认为它比UItableVIEw更少或更多
> CoreData集没有问题,它只是动画/ UI方面的东西(事情被删除)
>这是断断续续的,仅限于一些删除
>在来自railwayparade的一些帮助之后,我在dIDChangeObject中实现了NSFetchedResultsChangeMove:它修复了移动错误但没有修复删除错误.
谁能看到我错过的任何东西,或者我可以查看?很高兴提供更多信息,如果这有助于解决问题.
对于此处发布的淫秽数量的代码表示歉意.
错误:
CoreData: error: SerIoUs application error. An exception was caught
from the delegate of NSFetchedResultsController during a call to
-controllerDIDChangeContent:. attempt to insert row 3 into section 0,but there are only 3 rows in section 0 after the update with userInfo
(null)
//// MakeSentencetableVIEwController.h// StoryBot//// Created by Glen Storey on 25/10/10.// copyright 2010 Glen Storey. All rights reserved.//#import <UIKit/UIKit.h>#import "AddStoryItem.h"#import "Story.h"#import "Sentence.h"@interface MakeSentencetableVIEwController : UItableVIEwController <NSFetchedResultsControllerDelegate,AddStoryItemDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextFIEldDelegate,UIPopoverControllerDelegate> { NSManagedobjectContext *managedobjectContext; NSFetchedResultsController *fetchedResultsController; UIPopoverController *popoverController; UIbarbuttonItem *playbuttonItem; UIbarbuttonItem *addbuttonItem; BOol reordering; BOol insert; BOol delete; BOol move; int beginUpdatesCount; int endUpdatesCount;}@property (nonatomic,retain) Story *story;@property (nonatomic,retain) NSManagedobjectContext *managedobjectContext;@property (nonatomic,retain) UIbarbuttonItem *playbuttonItem;@property (nonatomic,retain) UIbarbuttonItem *addbuttonItem;@property BOol reordering,insert,delete,move;-(IBAction)createStoryModal:(ID)sender;-(voID)refreshtables;-(IBAction)pushShare:(ID)sender;@end//// MakeSentencetableVIEwController.m// //// Created by Glen Storey on 25/10/10.// copyright 2010 Glen Storey. All rights reserved.//#import "MakeSentencetableVIEwController.h"#import "ShareVIEwController.h"#import "StoryBotAppDelegate.h"@implementation MakeSentencetableVIEwController@synthesize story,managedobjectContext,addbuttonItem,playbuttonItem,reordering,move;-(voID)addStoryItemAction:(Nsstring*)text order:(NSNumber*)order image:(Nsstring*)image thumb:(Nsstring*)thumb{ NSLog(@"Text: %@,Order: %@,Image: %@.",text,order,image); NSLog(@"beginUpdatesCount: %d vs. endUpdatescount: %d",beginUpdatesCount,endUpdatesCount); NSSet *sentences = [story sentences]; NSNumber *maxOrder = [sentences valueForKeyPath:@"@max.order"]; NSLog(@"maxOrder: %@",maxOrder); if(maxOrder == 0){ maxOrder = [[NSNumber alloc] initWithInteger: 0]; } //make a new sentence! Sentence *sentence = [NSEntityDescription insertNewObjectForEntityForname:@"Sentence" inManagedobjectContext:managedobjectContext]; [sentence setText: text]; [sentence setimage: image]; [sentence setThumb: thumb]; [sentence setBelongsTo: story]; if([maxOrder intValue] >= 1 ){ [sentence setorder: [[NSNumber alloc] initWithInteger:[maxOrder intValue]+1]]; }else{ [sentence setorder: [[NSNumber alloc] initWithInteger:1]]; } NSMutableSet *mutableSet = [[NSMutableSet alloc] initWithSet:sentences]; [mutableSet addobject:sentence]; //NSLog(@"sentences before setWithSet %@",mutableSet); sentences = [[NSSet alloc] initWithSet: mutableSet]; //NSLog(@"sentences after setWithSet %@",sentences); [story setSentences:sentences]; //NSError *error; //BOol isSaved = [managedobjectContext save:&error]; //NSLog(@"isSaved? %@",(isSaved ? @"YES" :@"NO ") ); //if (!isSaved) { //NSLog(@"%@:%s Error saving context: %@",[self class],_cmd,[error localizedDescription]); //Don't worry about this warning - just rem it out when finished (just a log) // return; //} [sentences release]; [mutableSet release]; //[self.tableVIEw reloadData]; //[self.tableVIEw setNeedsdisplay]; [self dismissModalVIEwControllerAnimated:YES]; }#pragma mark -#pragma mark VIEw lifecycle-(ID)initWithNibname:(Nsstring*)name bundle:(NSBundle*)bundle;{ self = [super initWithNibname:name bundle:bundle]; if (self) { self.Title = @"My StorIEs"; addbuttonItem = [[UIbarbuttonItem alloc] initWithbarbuttonSystemItem:UIbarbuttonSystemItemAdd target:self action:@selector(createStoryModal:)]; playbuttonItem = [[UIbarbuttonItem alloc] initWithbarbuttonSystemItem:UIbarbuttonSystemItemPlay target:self action:@selector(pushShare:)]; if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) { [addbuttonItem setEnabled:NO]; [playbuttonItem setEnabled:NO]; } NSArray* toolbaritems = [NSArray arrayWithObjects: addbuttonItem,[[UIbarbuttonItem alloc] initWithbarbuttonSystemItem:UIbarbuttonSystemItemFlexibleSpace target:nil action:nil],nil]; [toolbaritems makeObjectsPerformSelector:@selector(release)]; self.toolbaritems = toolbaritems; //NSLog(@"self: %@ self.toolbaritems: %@",self,self.toolbaritems); //Do I need to release UIbarbuttonItems? NSLog(@"initWithNibname:"); } return self;}-(voID)refreshtables{ //use this to refresh for new table content. Also calls self.tablevIEw reloadData so you don't need to call it afterward. NSLog(@"===================================refreshtables"); NSLog(@"story object %@",story); if (managedobjectContext == nil) { NSLog(@"managedobjectContext == nil"); managedobjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedobjectContext]; NSLog(@"After managedobjectContext: %@",managedobjectContext); }else{ NSLog(@"managedobjectContext != nil"); } NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForname:@"Sentence" inManagedobjectContext:managedobjectContext]; [request setEntity:entity]; //sorting stuff: NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending: YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor,nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortDescriptor release]; nspredicate *predicateTitle = [nspredicate predicateWithFormat:@"belongsTo=%@",story]; [request setPredicate :predicateTitle]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"EEE,d MMM yyyy HH:mm:ss"]; Nsstring *dateString = [dateFormatter stringFromDate:[story creationDate]]; NSLog(@"dateString: %@",dateString); fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedobjectContext:managedobjectContext sectionnameKeyPath:nil cachename:dateString]; fetchedResultsController.delegate = self; [request release]; NSError *error; [fetchedResultsController performFetch:&error]; [self.tableVIEw reloadData];}- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; self.Title = @"My Story"; NSLog(@"Passed Story Object: %@",story); //NSLog(@"managedobjectContext: %@",managedobjectContext); //Need a predicate for belongsTo here. self.tableVIEw.rowHeight = 50; if(story != NulL){ if (managedobjectContext == nil) { NSLog(@"managedobjectContext == nil"); managedobjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedobjectContext]; NSLog(@"After managedobjectContext: %@",managedobjectContext); }else{ NSLog(@"managedobjectContext != nil"); } NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForname:@"Sentence" inManagedobjectContext:managedobjectContext]; [request setEntity:entity]; //sorting stuff: NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending: YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor,nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortDescriptor release]; nspredicate *predicateTitle = [nspredicate predicateWithFormat:@"belongsTo=%@",story]; [request setPredicate :predicateTitle]; fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedobjectContext:managedobjectContext sectionnameKeyPath:nil cachename:nil]; fetchedResultsController.delegate = self; [request release]; NSError *error; [fetchedResultsController performFetch:&error]; }#pragma mark -#pragma mark table vIEw data source- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw { // Return the number of sections. return 1;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section { //Return the number of rows in the section. NSArray *sections = [fetchedResultsController sections]; NSInteger count = 0; if ([sections count]){ ID <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section]; count = [sectionInfo numberOfObjects]; } NSLog(@"# of rows in section %d",count); return count;}// Customize the appearance of table vIEw cells.- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { static Nsstring *CellIDentifIEr = @"Cell"; UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if (cell == nil) { cell = [[[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr] autorelease]; } // Configure the cell... Sentence *sentenceAtCell = [fetchedResultsController objectAtIndexPath:indexPath]; //NSLog(@"sentenceAtCell: %@",sentenceAtCell); cell.textLabel.text = [sentenceAtCell text]; NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES); Nsstring *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[sentenceAtCell thumb]]; // This should crop it as you want - you've just got to create cropRect. UIImage *largeImage = [UIImage imageWithContentsOffile: uniquePath]; CGRect cropRect = CGRectMake(0,66,50); /*if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { if ([[UIScreen mainScreen] scale] == 2) { // running an iPhone 4 or equiv. res device. cropRect = CGRectMake(15,14,100,75); } }*/ CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage],cropRect); cell.imageVIEw.image = [UIImage imageWithCGImage: imageRef]; CGImageRelease(imageRef); //NSLog(@"indexPath: %@",indexPath); return cell;}// OverrIDe to support editing the table vIEw.- (voID)tableVIEw:(UItableVIEw *)tableVIEw commitEditingStyle:(UItableVIEwCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UItableVIEwCellEditingStyleDelete) { NSLog(@"Delete row"); NSfileManager *fileManager = [NSfileManager defaultManager]; NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES); Nsstring *documentsDirectoryPath = [paths objectAtIndex:0]; // 1. Look at the sentence we're about to delete. Sentence *sentenceRetire = [fetchedResultsController objectAtIndexPath:indexPath]; // 2. Does it have an order of 0? NSLog(@"=================== sentenceRetire: %@",sentenceRetire); if([[sentenceRetire order] intValue] == 0){ // YES: Is there another sentence in this story? Story *storyRetire = [sentenceRetire belongsTo]; NSSet *sentencesInRetiredSentenceSet = [storyRetire sentences]; if ([sentencesInRetiredSentenceSet count] > 1){ // YES: Set the sentence with the smallest order to an order of 0 // then delete the sentence + files nspredicate *predicateTitle = [nspredicate predicateWithFormat:@"order>0"]; NSSet *sentencesWithPotentialToBeTitle = [sentencesInRetiredSentenceSet filteredSetUsingPredicate:predicateTitle]; NSNumber *minorder = [sentencesWithPotentialToBeTitle valueForKeyPath:@"@min.order"]; nspredicate *predicateOrder = [nspredicate predicateWithFormat:@"order=%d",[minorder intValue]]; NSSet *sentenceWithPotentialToBeTitle = [sentencesWithPotentialToBeTitle filteredSetUsingPredicate:predicateOrder]; //note the sentence (singular) not sentences. This is the sentence who's order needs to be updated. NSLog(@"setenceWithPotentialToBeTitle (check order isn't null on crash): %@",sentenceWithPotentialToBeTitle); Sentence *sentencetoPromote = [sentenceWithPotentialToBeTitle anyObject]; //Now we kNow which sentence to promote we can delete the sentence & files. [managedobjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; Nsstring *imageTrash = [documentsDirectoryPath stringByAppendingPathComponent:(Nsstring*)[sentenceRetire image]]; Nsstring *thumbTrash = [documentsDirectoryPath stringByAppendingPathComponent:[sentenceRetire thumb]]; NSLog(@"About to delete these files: %@,%@",imageTrash,thumbTrash); [fileManager removeItemAtPath:imageTrash error:NulL]; [fileManager removeItemAtPath:thumbTrash error:NulL]; //and promote the new Title. [sentencetoPromote setorder:[[NSNumber alloc] initWithInteger:0]]; }else{ // NO: We're deleting this story // Delete the files! [managedobjectContext deleteObject:storyRetire]; Nsstring *imageTrash = [documentsDirectoryPath stringByAppendingPathComponent:(Nsstring*)[sentenceRetire image]]; Nsstring *thumbTrash = [documentsDirectoryPath stringByAppendingPathComponent:[sentenceRetire thumb]]; //NSLog(@"About to delete these files: %@,thumbTrash); [fileManager removeItemAtPath:imageTrash error:NulL]; [fileManager removeItemAtPath:thumbTrash error:NulL]; //Pop back. if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) { NSLog(@"last sentence in story - delete that story and point,somewhere!"); //probably delete the sentece to clear the table,then delete the story using storyRetire } else{ [self.navigationController popVIEwControllerAnimated:YES]; } } }else{ // NO: Delete the Sentence Object. [managedobjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; Nsstring *imageTrash = [documentsDirectoryPath stringByAppendingPathComponent:(Nsstring*)[sentenceRetire image]]; Nsstring *thumbTrash = [documentsDirectoryPath stringByAppendingPathComponent:[sentenceRetire thumb]]; //NSLog(@"About to delete these files: %@,thumbTrash); [fileManager removeItemAtPath:imageTrash error:NulL]; [fileManager removeItemAtPath:thumbTrash error:NulL]; } // Save the context. //NSError *error; //if (![managedobjectContext save:&error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipPing application,although it may be useful during development. If it is not possible to recover from the error,display an alert panel that instructs the user to quit the application by pressing the Home button. */ // NSLog(@"Unresolved error %@,error,[error userInfo]); // abort(); //} }}- (voID)setEditing:(BOol)editing animated:(BOol)animated{ [super setEditing:editing animated:animated]; if (!editing) { //save here NSError *error; BOol isSaved = [managedobjectContext save:&error]; NSLog(@"isSaved? %@ ======================================",(isSaved ? @"YES" :@"NO ") ); }}// OverrIDe to support rearranging the table vIEw.- (voID)tableVIEw:(UItableVIEw *)tableVIEw moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { //this implementation is from here: http://www.cimgf.com/2010/06/05/re-ordering-nsfetchedresultscontroller/ NSMutableArray *things = [[fetchedResultsController fetchedobjects] mutablecopy]; // Grab the item we're moving. NSManagedobject *thing = [fetchedResultsController objectAtIndexPath:fromIndexPath]; // Remove the object we're moving from the array. [things removeObject:thing]; // Now re-insert it at the destination. [things insertObject:thing atIndex:[toIndexPath row]]; // All of the objects are Now in their correct order. Update each // object's displayOrder fIEld by iterating through the array. int i = 0; for (NSManagedobject *mo in things) { [mo setValue:[NSNumber numberWithInt:i++] forKey:@"order"]; } NSLog(@"things: %@",things); [things release],things = nil; //reordering = YES; //NSLog(@"moveRowAtIndexPath: IS reordering"); //NSError *error; //[managedobjectContext save:&error]; }#pragma mark -#pragma mark table vIEw delegate/** Delegate methods of NSFetchedResultsController to respond to additions,removals and so on. */- (voID)controllerWillChangeContent:(NSFetchedResultsController *)controller { // The fetch controller is about to start sending change notifications,so prepare the table vIEw for updates. NSLog(@"controllerWillChangeContent: BeginUpdates"); [self.tableVIEw beginUpdates]; beginUpdatesCount++; NSLog(@"====================beginUpdates was just incremented"); NSLog(@"beginUpdatesCount %d",beginUpdatesCount); NSLog(@"endUpdatesCount %d",endUpdatesCount);}- (voID)controller:(NSFetchedResultsController *)controller dIDChangeObject:(ID)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { NSLog(@"dIDChangeObject: %@",anObject); UItableVIEw *tableVIEw; tableVIEw = self.tableVIEw; switch(type) { case NSFetchedResultsChangeInsert: NSLog(@"ResultsChangeInsert:"); insert = YES; [tableVIEw insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UItableVIEwRowAnimationFade]; break; case NSFetchedResultsChangeDelete: NSLog(@"ResultsChangeDelete:"); delete = YES; [tableVIEw deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UItableVIEwRowAnimationFade]; break; case NSFetchedResultsChangeMove: NSLog(@"ResultsChangeMove:"); move = YES; [tableVIEw deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UItableVIEwRowAnimationNone]; [tableVIEw insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UItableVIEwRowAnimationNone]; break; default: NSLog(@"switch problem - default"); }}- (voID)controller:(NSFetchedResultsController *)controller dIDChangeSection:(ID <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { NSLog(@"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(@"dIDChangeContent"); // The fetch controller has sent all current change notifications,so tell the table vIEw to process all updates. NSLog(@"almost endUpdates=============================================="); if(delete){ NSLog(@"endUpdates delete"); delete = NO; } if(move){ NSLog(@"endUpdates move"); move = NO; } if(insert){ NSLog(@"endUpdates insert"); insert = NO; } [self.tableVIEw endUpdates]; endUpdatesCount++; NSLog(@"====================endUpdates was just incremented"); NSLog(@"endUpdatesCount %d",endUpdatesCount); NSLog(@"beginUpdatesCount %d",beginUpdatesCount); NSLog(@"endUpdates finished");}#pragma mark -#pragma mark Memory management- (voID)dealloc { NSLog(@"Dealloc Sentence"); //[fliteEngine stopTalking]; //[fliteEngine release]; addbuttonItem = nil; playbuttonItem = nil; if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) { //It doesn't seem to get allocated because it's not set on the iPhone. [addbuttonItem release]; [playbuttonItem release]; } [story release]; [fetchedResultsController release]; [super dealloc];}@end解决方法 更新18/3#3我读了 this question关于fetchedResultsController需要在创建一个新委托之前将其委托设置为nil.我放
detailVIEwController.fetchedResultsController = nil;detailVIEwController.fetchedResultsController.delegate = nil;
在dIDselectrowatindexpath的SVC主视图中,问题已经停止.每次在主视图中选择一行时,Refreshtables都会创建一个新的fetchedResultsController,我认为即使选择了新的故事对象,它们仍然会干扰.
总结以上是内存溢出为你收集整理的ios – UITableView删除/添加行导致CoreData:严重应用程序错误,如果在SplitViewController的MasterView中选择了另一个对象全部内容,希望文章能够帮你解决ios – UITableView删除/添加行导致CoreData:严重应用程序错误,如果在SplitViewController的MasterView中选择了另一个对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)