core data delete crash - iphone

I need to update a task before it is deleted. I found when this line [self.fetchedResultsController objectAtIndexPath:indexPath]; is executed in NSFetchedResultsChangeDelete the app crashes.
case NSFetchedResultsChangeDelete:{
Task *task = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self deleleReminderForTask:task];
[self checkForUpdateForTaskForDelete:task];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
crash log:
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** -[_PFBatchFaultingArray objectAtIndex:]: index (40324416) beyond bounds (1) with userInfo (null)
Can anybody help me to fix this.

The fetched results controller has already been updated so you can't try to get the item from it. Instead you should use the object that is passed as a parameter to the delegate method. You should also check what you're doing with the deleted item is you still have issues.

Related

app crashes when deleting an object from core data in iphone

I have populated a table view with some data from server and saved it to the core data.Now i have to delete the object from the core data when the user clicks on the delete option in the table view.
What i have tried is this:`
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSError *error;
[[Server serverInfo].context deleteObject:[self.couponList objectAtIndex:indexPath.row]];
if(![ [Server serverInfo].context save:&error]) {
// Handle error
NSLog(#"Unresolved error series %#, %#", error, [error userInfo]);
}
[self.couponList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
if ([self.couponList count]==0) {
[self.table setEditing:NO animated:YES];
[self.editBt setStyle:UIBarButtonItemStyleBordered];
}
}
`
But it gives an exception and crashes.This is i am getting in the log :"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObjectContext cannot delete objects in other contexts".'Can anyone solve this?Thanks in advance
you have to make some manageobject context, a fetch request and then remove object using some predicate.
Apparently you are using more than one managed object context. This is indicated by your error message. Make sure that you are using only one managed object context, i.e. that there are no background tasks that use a different one.
You are keeping the data for your table view in a separate array. This might be another problem. The proper way to deal with core data and table views is to employ the NSFetchedResultsController.
Agree with Mundi.
On top of that, if you require many instances of the managedObjectContext, do not create that many but rather use the lock and unlock functions of the NSManagedObjectContext to enable multi-threading without issues on faults and invalidation of the objects.
EDIT
Maybe you can try creating just one NSManagedObjectContext in the AppDelegate and call the same managedObjectContext from the controllers where you need to use them. This, and together with the lock and unlock methods solved my issue with the multi-threading and invalidation of the objects.
Hope this helps.

NSRangeException causing app to crash on the phone but not the emulator

First of all Im going to show you the error I am getting, then I will try to explain what I am doing to get this error etc
2011-11-02 10:17:57.665 code[1327:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (0) beyond bounds (0).'
*** First throw call stack:
(0x31fe88bf 0x31d581e5 0x31fe87b9 0x31fe87db 0x32b6df1f 0x29c9b 0x32ae26b5 0x32b3daf1 0x32afed21 0x32afea71 0x32afe78b 0x32afe4ff 0x32ab581b 0x32abafb9 0x3193aba7 0x3273be8d 0x31fbb2dd 0x31f3e4dd 0x31f3e3a5 0x3204afed 0x32ace743 0x35a1 0x301c)
terminate called throwing an exception(gdb)
This is what I am doing to get this error.
Any time that I enter the subview with the dataset of indexpath[0,1] for the second time the app will throw this error.
This is the code where the error happen.
subview.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Center previously selected cell to center of the screen
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; //happens on this line
}
and this is the output from that error.
What should I be looking at.. I dont really know whats good or bad from this.
Whats really frustrating is that if I go in and out of IndexPath [0,0] from the main view or any of the other ones it works fine and there is never a problem with this app..
I am hoping someone can help me, or at the very least give me an idea of what the problem might be or where.
I had a similar problem in my project where I was deleting objects from my datasource and then immediately calling [self.tableview reloadData];
What I did to solve the issue was call:
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
[self.tableView reloadData];
});
This basically calls reloadData in the next run loop. You can do a similar thing using performSelector:afterDelay.
So my suggestion is try calling:
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
[self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
Note: You don't need to actually have a delay time specified, these 2 techniques force the method to be called at the start of the next run loop.

NSFetchRequest cause SIGABRT OR EXC_BAD_ACCESS

I'm using this simple code for my fetch request
NSArray *fetchResults = [moc executeFetchRequest:request error:&error];
NSLog(#" i want show my result : %#",fetchResults); -> cause SIGABRT
If i'm using on my persistent store just after this creation, i have an error.
PS: the store was save between the populate and the request.
But if i close the app, and reopen ( in this case the store exist), i have no error.
in some case i can view this message : terminate called after throwing an instance of 'NSException'
but i can't access to this exception.
if i count the fetch results, i have a good number, it's really strange.
Thanks for help.
Okay, I have found the problem!
In the populate code, one of my relationships was insert with an autorelease.
Remove this, and now it's OK.
This is not a good solution:
NSManagedObject *relationEntity = [[NSEntityDescription insertNewObjectForEntityForName:#"picture" inManagedObjectContext:moc] autorelease];
Simply remove autorelease:
NSManagedObject *relationEntity = [NSEntityDescription insertNewObjectForEntityForName:#"picture" inManagedObjectContext:moc];
I have forgotten this in core data (don't use release, just set object to nil)!

Scroll table to top causes crash

I've tried:
- (IBAction)openSearch {
[tblSimpleTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[searchBar becomeFirstResponder];
}
and
- (IBAction)openSearch {
[self.tblSimpleTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[searchBar becomeFirstResponder];
}
to get the table to scroll to the top. Both work when the table is only slightly scrolled away from the top but otherwise crashes with a "beyond bounds" error.
Any ideas. I am fairly new to this. Thanks.
--Edit--
Thanks for the feedback. Here's the precise error
2010-02-15 00:49:02.010 MyApp [2935:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
2010-02-15 00:49:02.016 MyApp [2935:207] Stack: (
861696817,
860329709,
861252493,
861252395,
845801683,
845954223,
30161,
835250561,
835249847,
834989551,
834983899,
834971003,
805539851,
805539363,
805538115,
805537449,
805560369,
861158231,
861448761,
861447005,
861059891,
861060063,
834770799,
834765939,
10065,
9980
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
I don't get this problem when I scroll manually. The table is populated from an NSMutableArray. Does this help at all?
Neither of those will cause a crash on their own, so your crash is a side-effect of the scrolling and not a direct result of the code you pasted. It would help to know the exact error you're seeing, but a likely cause is that you have an NSArray and you're trying to get an element at an index too large for it (or possibly negative). It's likely that one of your table view's delegate or data source methods are the direct cause of the crash, and that scrolling is only relevant because it causes a bug there to manifest itself.
Perhaps a bit of a fudge but it works if i clear the table first:
searching=YES;
[tblSimpleTable reloadData];
[tblSimpleTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
for searching=yes the table is set to reload and empty array. I guess I could repopulate the table after doing this but I don't really need to in this case. I think the problem had something to do with the table cells being redrawn when they came into view to save memory.
Anyhow - for people with the same problem: reload the table with an empty array, scroll to the top, reload the table again with the old array. Hope this helps. Maybe someone will come up with a better way to do this.

Core Data error when deleting row in tableView

I have a UITableViewController managing a grouped tableView. The tableView is populated from a fetchedResultsController.
If I click the Edit button in the NavigationBar, then select a row and click the Delete button, the row is deleted and all ends well.
However, if I swipe to reveal the Delete button in a row and click the Delete button, the app crashes with the following error:
2010-01-06 15:25:18.720 Take10[14415:20b] Serious application error. Exception was caught during Core Data change processing: -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
2010-01-06 15:25:18.721 Take10[14415:20b] Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
Of course, the index number in the error changes depending on the number of rows in the section where I am attempting to delete a row, and that number is 1 more than the number of remaining rows in the table section after the attempted delete.
Here is the code where I attempt to delete the data from the fetchedResultsController. The same method responds to both scenarios, so I don't understand why it crashes when swiping.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context 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();
}
}
}
Any ideas???
Thanks
Jk
Jeff LaMarche has done some good work with NSFetchedResultsController.
Try using his template here:
http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html
And see if that solve your issue.
One difference between normal and swipe delete is that the latter will call tableView:willBeginEditingRowAtIndexPath and tableView:didEndEditingRowAtIndexPath. In fact, that's a good way of suppressing the indentation and showing of an insert row.
Another difference is that setEditing: is called (with NO as the parameter value) immediately after the delete.
Set breakpoints in any of those three functions that you've defined/overridden and see if you can narrow down where it's happening.