So I'm a bit dismayed that Apple has replaced a transparent, easy-to-handle NSUInteger
with an HKQueryAnchor
but has not provided an easy way to persist that HKQueryAnchor
. Has anyone found a good way to do this with NSUserDefaults
? The only persistence method I have seen is an archiver to a local file, but my app persists everything in NSUserDefaults
, and I'd like to keep it that way if possible. Is there a reliable way to store an HKQueryAnchor
this way?
You can convert it to NSData
using NSKeyedArchiver
and store that in the user defaults.
To store:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:anchor];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"AnchorData"];
To retrieve:
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"AnchorData"];
HKQueryAnchor *anchor = [NSKeyedUnarchiver unarchiveObjectWithData:data];