IPhone Core Data does not update table

I am trying to write an application with Core Data, and I was able to successfully read and write to the base database. However, if I write to the database in one view controller, my other view controllers will not see the changes until the application is closed and then opened again. This is really frustrating. I'm not quite sure how to make the updated - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flagmethod work. How to get a link to my managed entity?

Anyway, here is the code that I use 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 the help in advance!

+3
2

, , , . , .

-1

, , , , , , ?. NSManagedObjectContextDidSaveNotification ( ) , :

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

UpdateTable myController :

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

, .

-Oscar

+3

Source: https://habr.com/ru/post/1730562/


All Articles