Kurt
Kurt

Reputation: 137

Core Data not updating until after application terminate

I am having an issue with Core Data... here is my situation:

I have a Restaurant and that restaurant has a category which is a to-many relationship and the category also has a to-many relationship back to the restaurants for example:
McDonald's has a category of: (category) Fast Food, (category) Local

Category (Fast Food)
Type: (NSString) Fast Food)
Restaurant: (Restaurant) McDonald's, (Restaurant) Burger King ... etc

So this works. I can have it show this data, add to it, delete etc. The issue I am having: I cannot get my results to show up until I actually stop and restart my applications. I cannot seem to 'refresh' my managedObject with the new data.

For Example: Currently McDonald's has a Category of Fast Good and Local, but in my application I want the user to be able to add another category. So the user goes in, sees a list of categories and then selects the category "Lunch". This is the code I use to do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"EditCatagoriesTableViewController - didSelectRowAtIndexPath section:%d row:%d ",indexPath.section,indexPath.row);

    if ([tableView cellForRowAtIndexPath:indexPath].accessoryType != UITableViewCellAccessoryCheckmark){

        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;  
        [restaurant addCategoriesObject:(Category*)[categoryArray objectAtIndex:indexPath.row]];
    }else{
        if ( [currentCategories  containsObject:[categoryArray objectAtIndex:indexPath.row]] )
            [restaurant removeCategoriesObject:(Category*)[categoryArray objectAtIndex:indexPath.row]];

        }else{
            NSLog(@"Object Not Present");
        }
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;  

    }

    NSError *error = nil;
    if (![ [restaurant managedObjectContext] save:&error]) {
        // Handle error
        NSLog(@"restaurant - Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }       
    if (![ [[categoryArray objectAtIndex:indexPath.row] managedObjectContext] save:&error]) {
        // Handle error
        NSLog(@"restaurant - Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

This works only in the effect if I shut down my app and reload it. But If I use my restaurant object (which is a managedObject for McDonald's) it does not show up until that application shut down, I have opened a break point and in gdb have done this:

(gdb) po restaurant
<Restaurant: 0x114cf80> (entity: Restaurant; id: 0x114b660 <x-coredata://9477D580-A02E-482C-AF7B-F01D6E9AC332/Restaurant/p2> ; data: {
    categories =     (
        0x131f560 <x-coredata://9477D580-A02E-482C-AF7B-F01D6E9AC332/Category/p7>
    );
    city = "Cedar Park";
    deals =     (
    );
    foodTypes =     (
    );
    name = "Mc Donalds";
    phoneNumber = "(512)336-0323";
    state = TX;
    "street_1" = "1103 N Bell Blvd";
    "street_2" = "";
    timeStamp = nil;
    webAddress = "http://www.mcdonalds.com/";
    zip = 78613;
})

As you can see it shows the Categories as the one (in this case only has one category linked to this restaurant)

<Category: 0x132d9c0> (entity: Category; id: 0x132cf80 <x-coredata://9477D580-A02E-482C-AF7B-F01D6E9AC332/Category/p4> ; data: {
    Restaurant =     (
        0x114b660 <x-coredata://9477D580-A02E-482C-AF7B-F01D6E9AC332/Restaurant/p2>
    );
    name = "Weekday Lunch Spot";
})

Yet my Category (lunch spot) shows it has a pointer back to the Restaurant Mc Donald's

So How do I go about refreshing the restaurant to that I can see the new object?

Upvotes: 1

Views: 566

Answers (2)

aahsanali
aahsanali

Reputation: 3547

I was also facing the same problem that core data wasn't updating unless I restart the application but after long struggle I abled to fix this by resetting the managed object context

[[self managedObjectContext] reset];

Upvotes: 0

Jim Zajkowski
Jim Zajkowski

Reputation: 987

Almost always this is a result of KVO not observing the change. Are you directly modifying objects or using the KVC methods to set attributes?

Upvotes: 1

Related Questions