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.

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.

I am not really sure what you are asking. You want to add another image to the RecentImagesArray when someone looks at an image?
–
drewagJun 18 '11 at 17:27

@drewag Yes. I'm using a tabbed controller. One is Places, the other "recent images". When you switch to the places tab, you can browse the top 100 places on flickr and look at images. When you view an image in Places, it updates the array that is stored in NSUserDefaults. I would like to have these updates show on the Recent Images table when you switch views.
–
xianritchieJun 18 '11 at 18:22

1 Answer
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.

I think I'm actually doing Option 1, partially. Each time an image is selected in the list, it saves that images data to defaults. However, the issue I'm having is refreshing it after its been updated. I thought that pulling the defaults in viewWillAppear would read from the defaults each time I switched to Recent Images, but that is not the case. Since I'm reading NSUserDefaults into a variable (NSArray *recentImagesArray), I'd need to update that when NSUserDefaults changed.
–
xianritchieJun 19 '11 at 20:34

1

Annnnd, I ended up figuring it out. My code was correct. I didn't have [self.tableView reloadData] called at any point in the code, so it wasn't refreshing the table data. I would +1 you if I could.
–
xianritchieJun 19 '11 at 20:42