Rits
Rits

Reputation: 5175

How to save and retry reporting GKAchievement's after network failure?

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?

Upvotes: 4

Views: 883

Answers (2)

David Dunham
David Dunham

Reputation: 8329

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

Upvotes: 0

Cory Kilger
Cory Kilger

Reputation: 13044

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

Upvotes: 5

Related Questions