0

I have UITableView with NSFetchedResultsController and a main context attached to it. I'm updating my main context from background.

- (void)contextDidSave:(NSNotification *)notification
{
    NSManagedObjectContext *context=[self managedObjectContext];
    [context performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES];

    if ([context hasChanges])
    {            
        [context performSelectorOnMainThread:@selector(save:) withObject:nil waitUntilDone:YES];

    }        
}

My UITableView reloads with new data and everything is fine, but some UITableView cell's data must be populated from fetched properties and I need that values to be refreshed.

I've read about NSManagedObjectContext's method refreshObject:mergeChanges: that might help. Where should I place it? If I will place it in cellForRowAtIndexPath's helper method - configureCell:atIndexPath: this will result in cycling row updates.

Here's the piece of cell configuration code:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)theIndexPath
{
    //NSLog(@"PTAgencyList: configureCell: atIndexPath: %i %i", theIndexPath.section, theIndexPath.row);

    NSManagedObject *managedObj=[self.agenciesFetchedResultsController objectAtIndexPath:theIndexPath];
    if (managedObj==nil)
    {
        NSLog(@"PANIC: fetchedResultsController objectAtIndexPath: returned nil object");
        return;
    }
    else
    {

        CDAgency *agency=(CDAgency *)managedObj;

        [agency city];
        NSLog(@"agency.city_server_id: %@", agency.city_server_id);
        NSLog(@"agency.city.name: %@", ((CDCities *)[[agency city] objectAtIndex:0]).name);
        NSFetchRequest *fr=[[NSFetchRequest alloc] init];
        [fr setEntity:[NSEntityDescription entityForName:@"Cities" inManagedObjectContext:self.context]];
        [fr setPredicate:[NSPredicate predicateWithFormat:@"server_id=%@", agency.city_server_id]];
        NSArray *res=[self.context executeFetchRequest:fr error:nil];
        NSLog(@"fetched city name: %@", ((CDCities *)[res objectAtIndex:0]).name);

After context update: fetched property city is nil (I think it is because it still points to the old city entry that was removed), but if I'm doing the fetch manually I'm getting fresh data.

4

1 回答 1

1

只需在配置单元格的位置显式调用获取的属性即可。

[managedObject fetchedPropertyAttribute];

它应该按预期重新计算。

此外,请考虑使用关系而不是获取的属性。

于 2013-04-17T11:55:48.503 回答