当 NSUserDefaults 更新时更新 NSArray`

发布于 2024-11-16 02:37:21 字数 1992 浏览 2 评论 0原文

我有一个应用程序,可以提取 flickr 上热门地点的列表,并返回发布到这些地点的最近照片。这一切都是动态完成的。我还将用户查看过的最近图像列表存储到 NSUserDefaults 中。 flickr 提取图像所需的数据存储在 NSDictionary 中,因此我将 NSDictionary 写入数组,然后将所述数组存储在 NSUserDefaults 中。这是一个学习项目,所以我需要使用 NSUserDefaults,即使可能有更好的方法。

至此,几乎一切都运行良好。但是,我对用户在动态视图中查看新图像后更新最近图像的方法感到困惑。 NSUserDefaults 更新没有问题,并且附加的最近图像显示我是否关闭应用程序并重新启动它。

这是我写入默认值的代码。这里似乎不是问题。

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

这是我正在读取数据的地方。

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

老实说,任务的要求已经满足,如果我愿意的话,我可以在这里停下来。但我想了解这是如何完成的。我非常擅长推断通用代码示例并将其应用到我的示例中,因此您不必解决我的特定问题。只需知道在默认值更新后如何更新从用户默认值设置的变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2024-11-23 02:37:21

选项 1

您可以做的一件事是每次查看新图片时保存到 userDefaults,然后在选择“最近的图像”选项卡时从默认值重新加载。

为了提高效率,您可以保存一组新查看的图像,并仅在离开选项卡时将它们保存到 userDefaults 中。

选项 2

使用在 NSNotificationCenter 上添加一个观察者来监视“NSUserDefaultsDidChangeNotification”。这样,您就可以运行一个函数,在每次 UserDefaults 更改时更新最近的图像。

选项 3

创建您自己的通知,名称类似于“ImageViewed”。您可以注册最近的图像以收听来自 NSNotificationCenter 的通知,并且每当查看新图像时,您都会将该通知发布到通知中心。

如果您需要对这些方法中的任何一个进行澄清,我可以尝试进一步解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文