views:

183

answers:

2

Hi all,

I'm trying to write an application with Core Data, and I have been able to successfully read and write to the core data database. However, if I write to the database in one view controller, my other view controllers will not see the change until the app is closed then reopened again. This is really frustrating. I'm not entirely sure how to get the refresh - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag method to work. How do I get a reference to my managed object?

Anyways, here's the code I'm using to read the data back. This is in the viewDidLoad method.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
    //Handle the error
}

[self setNewsTitlesArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

[newsSourcesTableView reloadData];

Thanks for help in advance!

+1  A: 

I am not entirely sure what do you intend to do from what I understand you are changing your managed object context in one view controller and you want the result to be visible in other view controllers is this correct?. Anyway a solution for this is to listen for the NSManagedObjectContextDidSaveNotification (which is send when the context is saved) and register as a observer the view controller you want to be afected by the changes:

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:myController selector:@selector(updateTable:) name:NSManagedObjectContextDidSaveNotification object:controller.context];

The updateTable in the myController Controller the selector could look something like this:

- (void)updateTable:(NSNotification *)saveNotification
{
if (fetchedResultsController == nil)
{
    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
    //Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }       
}
else
{
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    // Merging changes causes the fetched results controller to update its results
    [context mergeChangesFromContextDidSaveNotification:saveNotification];  
    [self.tableView reloadData];
}

Hope that helps.

-Oscar

OscarMk
A: 

Actually, what I found was that I had the table set to load as my view. When I put the table into another view, everything worked fine.

Brian515