Question

So i can't seem to get any data to display in my tableview but then i noticed the context was not saving and i kept getting this error:

[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x166a70b0) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING
2014-04-12 19:49:04.722 0DataCollector[3218:60b] Managedcontext working (
    TestSuburb
)



- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }

Any help you can offer would be greatly appreciated, i have also tried saving "contextWithinCurrentThread" which didn't help.

Rest of code for TableViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    suburbsArray = [[NSMutableArray alloc] init];

    [self fetchSuburbs];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)fetchSuburbs {
    //Fetch suburbs
    self.suburbsArray = [NSMutableArray arrayWithArray:[Suburb findAllSortedBy:@"name" ascending:YES]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self fetchSuburbs];

    [self.tableView reloadData];
}

#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.
    return [suburbsArray count];
}


- (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];
    }

    NSMutableArray *reversedSuburbsArray = [[[suburbsArray reverseObjectEnumerator] allObjects] mutableCopy];

    //Fetch suburb
    Suburb *suburb = [self.suburbsArray objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = suburb.name;

    return cell;
}
Was it helpful?

Solution

Perhaps you are not creating and inserting a new NSmanagedObject "suburb" into the context and so there is no change. In this case your issue is not with the save, there is nothing to save.

Adding to the mutable array does not make for a new suburb created. You have only created a string object unrelated to any NSmanagedObject.

I then suggest:

- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {

    if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
        NSLog(@"Coming back from NewSuburbTableViewController");
        NewSuburbTableViewController *srcVC = segue.sourceViewController;
        suburbString = srcVC.suburbTextField.text;
        [suburbsArray addObject:suburbString];

        // Create the new suburb
        Suburb *theNewBurb = [Suburb createEntity];
        theNewBurb.burbName = suburbString; // I don't know what properties you have on Suburb so you would need to correct this
        // You also need to set any other required properties of Suburb prior to save or it will fail

        //Save Managed Object Context
        [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"You successfully saved your context.");
            } else if (error) {
                NSLog(@"Error saving context: %@", error.description);
            }
        }];        NSLog(@"Managedcontext working %@", suburbsArray);


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top