网络故障后如何保存并重试报告 GKAchievement?

发布于 2024-09-27 14:03:52 字数 194 浏览 3 评论 0原文

苹果表示,如果你想报告 GKAchievement 但遇到网络错误,处理此问题的最佳方法是保存 GKAchievement(可能将其添加到数组中),然后定期尝试报告成就。

保存成就的最佳位置在哪里? NSUserDefaults 就足够了,还是属性列表是更好的方法?

我应该何时以及多久尝试报告一次?应用程序启动时,还是每隔 10 分钟一次?

Apple states that if you want to report a GKAchievement but you get a network error, the best way to handle this is to save the GKAchievement (possibly adding it to an array), then periodically attempt to report the achievement.

What is the best place to save the achievements? Would NSUserDefaults suffice, or would a property list be a better way?

When and how often should I attempt to report? On application launch, or something like every 10 minutes?

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

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

发布评论

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

评论(2

瀟灑尐姊 2024-10-04 14:03:52

属性列表只能处理特定的类(请参阅 "什么是属性列表?"),GKAchievement 不是其中之一。 NSUserDefaults 使用属性列表,因此这也不再适用。但是,GKAchievement 确实符合 NSCoding 协议,这意味着您可以使用 NSKeyedArchiver 轻松将它们保存到磁盘。我会创建一系列未报告的成就并像这样读/写它们:

//Assuming these exist
NSArray * unreportedAchievements;
NSString * savePath;

// Write to disk
[NSKeyedArchiver archiveRootObject:unreportedAchievements toFile:savePath];

// Read from disk
unreportedAchievements = [NSKeyedUnarchiver unarchiveObjectWithFile:savePath];

A property list can only handle specific classes (see "What is a Property List?"), which GKAchievement is not one of. NSUserDefaults uses property lists, so that's also out. GKAchievement does, however, conform to the NSCoding protocol, which means you can easily save them to disk using an NSKeyedArchiver. I would create an array of unreported achievements and read/write them like so:

//Assuming these exist
NSArray * unreportedAchievements;
NSString * savePath;

// Write to disk
[NSKeyedArchiver archiveRootObject:unreportedAchievements toFile:savePath];

// Read from disk
unreportedAchievements = [NSKeyedUnarchiver unarchiveObjectWithFile:savePath];
弱骨蛰伏 2024-10-04 14:03:52

您几乎可以通过将属性列表(以及 NSUserDefaults)中的任何内容转换为 NSData:archivedDataWithRootObject:

You can pretty much save anything in a property list (and thus NSUserDefaults) by turning it into NSData: archivedDataWithRootObject:

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