2010-05-23 66 views
0

我已經有了一個基本的相冊應用程序,在第一個視圖中顯示的相冊列表中顯示的字幕顯示每個相冊中有多少圖像。我已經有一切工作來添加專輯,並將圖像添加到相冊中。 問題是,無論何時加載應用程序,圖像計數行都是準確的,但我無法在執行過程中讓它們更新。數據不會從次要更新的核心數據重新加載到桌面視圖

下viewDidLoad中正確填充tableview中的所有行,當應用程序加載:

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

但當我裏面viewdidappear運行類似的代碼,什麼都不會發生:

{ 
    /* 
    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]; 
} 

道歉,如果我有在其他地方錯過了這個問題的答案,但是我錯過了什麼?

回答

2

要排除故障,請在-viewDidAppear:的幾個地方放置NSLog報表。您可以使用這些日誌語句來確保此方法被正確調用並至少檢查mutableFetchResults的內容。

+0

謝謝,我沒有保存在運行時存儲的數據,我只是在退出時保存更改。現在工作正常。 – 2010-05-23 20:13:42