xianritchie
xianritchie

Reputation: 331

Updating an NSArray when NSUserDefaults is updated`

I have an application that pulls a list of the top places on flickr and returns the recent photos posted to those places. That is all done dynamically. I am also storing a list of recent images the user has viewed to NSUserDefaults. The data that flickr needs in order to pull up an image is stored in an NSDictionary, so I am writing the NSDictionary to an array, and then storing said array in NSUserDefaults. This is for a learning project, so I'm required to use NSUserDefaults, even if there is likely a better way.

At this point, ALMOST everything works great. However, I am flummoxed on a method of updating the Recent Images after a user views a new image in the dynamic views. NSUserDefaults updates without issue, and the additional recent images show if I shut down the app and restart it.

Here is my code where I write to defaults. Doesn't seem to be an issue here.

idvc = [[ImageDisplayViewController alloc] init];
idvc.flickrInfo = [placePhotosArray objectAtIndex:[indexPath row]];

// Add data to NSUserDefaults

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataFromRecentArray = [currentDefaults objectForKey:@"recentImages"];

if (dataFromRecentArray != nil)
{
    NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromRecentArray];
    if (oldArray != nil)
    {
        currentRecentImagesArray = [oldArray mutableCopy];
    } else {
        currentRecentImagesArray = [[NSMutableArray alloc] init];
    }
} else {
    currentRecentImagesArray = [[NSMutableArray alloc] init];
}

[currentRecentImagesArray addObject:idvc.flickrInfo];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:currentRecentImagesArray] forKey:@"recentImages"];
[currentRecentImagesArray release];
[self.navigationController pushViewController:idvc animated:YES];
[idvc release];

Here is where I am reading the data back out

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
   NSData *dataFromRecentImages = [currentDefaults objectForKey:@"recentImages"];
   if (dataFromRecentImages != nil) {
    NSArray *currentArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromRecentImages];
    recentImagesArray = [[NSArray alloc] initWithArray:currentArray];
   }
}

Honestly, the requirements for the assignment are fulfilled, I could stop here if I wanted to. But I'd like to understand how this is done. I am pretty good at extrapolating generic code examples and applying it to mine, so you don't have to fix MY specific problem. Just need to know how to update a variable that's value is set from user defaults after defaults have been updated.

Upvotes: 0

Views: 405

Answers (1)

drewag
drewag

Reputation: 94803

Option 1

One thing you can do is save to the userDefaults every time a new picture is looked at and then when the "Recent Images" tab is selected, reload from the defaults.

To make that a little more efficient, you could save an array of newly viewed images, and only save them to the userDefaults when leaving the tab.

Option 2

Use add an observer on NSNotificationCenter watching "NSUserDefaultsDidChangeNotification". That way you can run a function that updates the recent images every time the UserDefaults changes.

Option 3

Create your own notification called something like "ImageViewed". You can register the recent images to listen to that notification from NSNotificationCenter and whenever a new image is viewed, you post that notification to the notification center.

If you need some clarification on any of those methods, I can try to explain it further.

Upvotes: 1

Related Questions