[英]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. 我有一个应用程序,可以提取flickr上排名靠前的位置,并返回发布到这些位置的最近照片。 That is all done dynamically.
这都是动态完成的。 I am also storing a list of recent images the user has viewed to NSUserDefaults.
我还存储了用户已查看到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.
flickr为了提取图像所需的数据存储在NSDictionary中,因此我将NSDictionary写入数组,然后将所述数组存储在NSUserDefaults中。 This is for a learning project, so I'm required to use NSUserDefaults, even if there is likely a better way.
这是一个学习项目,因此即使有更好的方法,我也必须使用NSUserDefaults。
At this point, ALMOST everything works great. 至此,ALMOST一切正常。 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.
NSUserDefaults更新没有问题,并且其他最新图像显示了我是否关闭应用程序并重新启动它。
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.
仅需要知道如何在默认值已更新后更新由用户默认值设置的变量即可。
Option 1 选项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. 您可以做的一件事是每次查看新图片时将其保存到userDefaults,然后在选择“ Recent Images”选项卡时,从默认值重新加载。
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. 为了提高效率,您可以保存一系列新查看的图像,并且仅在离开选项卡时将其保存到userDefaults中。
Option 2 选项2
Use add an observer on NSNotificationCenter watching "NSUserDefaultsDidChangeNotification". 使用在NSNotificationCenter上添加观察者,观察“ NSUserDefaultsDidChangeNotification”。 That way you can run a function that updates the recent images every time the UserDefaults changes.
这样,您可以运行一个函数,每次UserDefaults更改时都会更新最近的图像。
Option 3 选项3
Create your own notification called something like "ImageViewed". 创建自己的名为“ 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.
您可以注册最近的图像以从NSNotificationCenter收听该通知,并且每当查看新图像时,都可以将该通知发布到通知中心。
If you need some clarification on any of those methods, I can try to explain it further. 如果您需要对这些方法中的任何一种进行澄清,我可以尝试进一步解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.