Note that there are some explanatory texts on larger screens.

plurals
  1. POI am trying to read and then update a value using Core Data, but when I run it, I get “EXC_BAD_ACCESS”, but when I debug it, it works
    text
    copied!<p>I am trying to get the hang of Core Data, and I am running into a strange issue. I have an app that uses a local notification to alert the user, whereupon the user enters my app and this method is called:</p> <pre><code>- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif { </code></pre> <p>When the app loads, I prompt another alert view to get some information from the user:</p> <pre><code>UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@" Did you %@?", notif.alertBody] message:@"" delegate:self cancelButtonTitle:@"No..." otherButtonTitles:@"Yes!", nil]; [alert show]; </code></pre> <p>And then every time this alert is shown, I want to increment a counter value in my Core Data DB, and I do that by calling this method right after I show the alert:</p> <pre><code>[self incrementVal]; </code></pre> <p>Inside of this method, I read a value, turn it into an int, then pass the new number off to another method that will then save the new value in my db. </p> <pre><code>-(void) incrementVal{ //for reading from the db NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"score" inManagedObjectContext:managedObjectContext_]; [fetchRequest setEntity:entity]; NSError *error; NSPredicate * pred = [NSPredicate predicateWithFormat:@"id == 2"]; [fetchRequest setPredicate:pred]; NSArray * strengthVal = [managedObjectContext_ executeFetchRequest:fetchRequest error:&amp;error]; armorScore * armo = [strengthVal objectAtIndex:0]; int aScor = [armo.score intValue]; aScor++; //For updating the db [self updateDBVal:aScor]; [fetchRequest release]; } -(void) updateDBVal:(int)value{ NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init]; NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"score" inManagedObjectContext:managedObjectContext_]; [fetchRequest2 setEntity:entity2]; NSError *error2; NSPredicate * pred2 = [NSPredicate predicateWithFormat:@"id == 2"]; [fetchRequest2 setPredicate:pred2]; NSArray * banna = [managedObjectContext_ executeFetchRequest:fetchRequest2 error:&amp;error2]; armorScore * bann2 = [banna objectAtIndex:0]; bann2.score = [NSString stringWithFormat:@"%d", value]; [managedObjectContext_ save:&amp;error2]; [error2 release]; [fetchRequest2 release]; } </code></pre> <p>So I don't know if this approach is ideal, but it is the one that I came up with. It runs fine when I debug it and go through each line, but when I just run it, it crashes saying Exc_Bad_Access...</p> <p>What am I doing wrong? I really want to get this figured out.</p> <p>Thanks!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload