views:

91

answers:

1

I've got a basic photo album application, on the first view a list of albums is displayed with a subtitle showing how many images are in each album. I've got everything working to add albums, and add images to albums. The problem is that the image count lines are accurate whenever the app loads, but I can't get them to update during execution.

The following viewdidload correctly populates all lines of the tableview when the app loads:

- (void)viewDidLoad {
    [super viewDidLoad]; 
    // Set the title.
    self.title = @"Photo albums";

    // Configure the add and edit buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAlbum)];
    addButton.enabled = YES;
    self.navigationItem.rightBarButtonItem = addButton;

    /*
    Fetch existing albums.
    Create a fetch request; find the Album entity and assign it to the request; add a sort descriptor; then execute the fetch.
    */
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    // Order the albums by name.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"albumName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    LocationsAppDelegate *mainDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];

    // Set master albums array to the mutable array, then clean up.
    [mainDelegate setAlbumsArray:mutableFetchResults];
    [mutableFetchResults release];
    [request release];
}

But when I run similar code inside viewdidappear, nothing happens:

{
    /*
    Fetch existing albums.
    Create a fetch request; find the Album entity and assign it to the request; add a sort descriptor; then execute the fetch.
    */
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    // Order the albums by creation date, most recent first.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"albumName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    LocationsAppDelegate *mainDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];

    // Set master albums array to the mutable array, then clean up.
    [mainDelegate setAlbumsArray:mutableFetchResults];
    [self.tableView reloadData];
    [mutableFetchResults release];
    [request release];  
}

Apologies if I've missed the answer to this question elsewhere, but what am I missing?

+2  A: 

To troubleshoot, put NSLog statements in a few places in -viewDidAppear:. You would use these log statements to make sure that this method is both being called properly and to examine the contents of mutableFetchResults, at least.

Alex Reynolds
Thanks, I'd not saved the data to store during runtime, I was only saving changes on exit. Works fine now.
Martin KS