Author Topic: CoreData error  (Read 6681 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
CoreData error
« on: August 18, 2014, 06:01:44 PM »
I really need help with this, which I can't seem to figure out the solution to. I'm working on a quiz app; and when I try to save the user's score using my business object, I get an error that I've localized to the saveEntities method in mmBusinessObject as shown in the attachment. In the console, I get a whole bunch of messages, and the first message in that long list is as follows:

2014-08-18 14:35:55.211 sidepanel[28768:60b] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null)

This is the code for my tableViewcontroller (MasterViewController1) where the error is occurring. The problem occurs at the second line that says [self.pugMenuBusinessObject saveEntities]; inside the method updateEntityGradeScore:from: where I try to save the self.pugMenuEntity after assigning the grade to its gradeScore attribute.

Any help or ideas on how to resolve this will be most appreciated.

Code: [Select]
//
//  MasterViewController1.m
//  sidepanel
//
//  Created by Richard Tamesis on 7/24/14.
//  Copyright (c) 2014 SkyPug. All rights reserved.
//

#import "MasterViewController1.h"
#import "PugMenuBusinessObject.h"
#import "PugMenuEntity.h"


@interface MasterViewController1 ()

@property (strong, nonatomic) PugMenuBusinessObject *pugMenuBusinessObject;
@property (strong, nonatomic) PugMenuEntity *pugMenuEntity;

@end

@implementation MasterViewController1


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self selectTabBarItemImage:0 withImageFileName:@"PhysicalSelected"];
    [self createBusinessObject];
}


#pragma mark - PUBLIC HELPER METHODS

- (void) createBusinessObject
{
    self.pugMenuBusinessObject = [[PugMenuBusinessObject alloc] init];
    self.fetchedResultsController = [self.pugMenuBusinessObject createAFetchedResultsControllerForEntity:@"PugMenuEntity" andSortDescriptorKey:@"section" ascending:YES];
   
}


// Delegate method from StartViewController
- (void) updateEntityGradeScore:(float)grade from:(StartViewController *)resultsVC
{
    self.pugMenuEntity.gradeScore = grade;

    [self.pugMenuBusinessObject saveEntities];
    self.cell.detailTextLabel.text = [NSString stringWithFormat:@"%0.1f%%", self.pugMenuEntity.gradeScore];
    [self.tableView reloadData];

}


#pragma mark - TABLE VIEW DATASOURCE METHODS
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
   
    self.pugMenuEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    if (self.totalNumberOfRows < 2) {
       
        self.cell.textLabel.text = [NSString stringWithFormat:@"%@", self.pugMenuEntity.topic];
       
    } else {
       
        self.cell.textLabel.text = [NSString stringWithFormat:@"%ld  %@", (long)(indexPath.row + 1), self.pugMenuEntity.topic];
       
    }
   
    if (!self.pugMenuEntity.gradeScore) {
        self.cell.detailTextLabel.text = @"";
    }
    else{
        self.cell.detailTextLabel.text = [NSString stringWithFormat:@"%0.0f%%", self.pugMenuEntity.gradeScore];
    }
   
    return self.cell;
}

#pragma mark - NAVIGATION METHODS
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
   
    self.pugMenuEntity = [self.fetchedResultsController objectAtIndexPath:self.selectedIndexPath];
       
    [self.startViewController valuesFromMasterViewController:self.pugMenuEntity.topic majorSubjectName:self.pugMenuEntity.tabName image:self.pugMenuEntity.imageName introductoryText:self.pugMenuEntity.introText andTabBarItemTitle:self.tabBarItem.title];
   
}



@end

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: CoreData error
« Reply #1 on: August 19, 2014, 01:59:01 AM »
Ok, I solved it by deleting the current SQLite database and basically creating a brand new one from scratch. I guess it got corrupted or something, because now my code works and I no longer get CoreData errors.

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: CoreData error
« Reply #2 on: August 19, 2014, 01:14:42 PM »
Glad you were able to figure it out!

Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: CoreData error
« Reply #3 on: August 19, 2014, 01:27:50 PM »
Spent hours going through everything trying to find out what I did wrong. Was there anything specific in the error messages that would have given me a clue as to what the problem was? I only deleted the database as a last resort.

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: CoreData error
« Reply #4 on: August 19, 2014, 08:00:41 PM »
No, the error was pretty generic, unless there was more information provided in the Console.

Kevin