How to serialize and store PHPhotoLibrary changes?

Hi, I am trying to store persistent changes to the photo library.

I know I can use a PHPersistentChangeToken to get the changes since that token, but I am not sure how to serialize this object and save it into a file.

I am also wondering if you can serialize a PHPersistentChangeFetchResult and store that in a file.

Cheers.

Replies

Since PHPersistentChangeToken conforms to NSSecureCoding you are able to serialize it with NSKeyedArchiver and NSKeyedUnarchiver. It would look something like this:

let token = PHPhotoLibrary().currentChangeToken
let data = try NSKeyedArchiver.archivedData(withRootObject: token, requiringSecureCoding: true)
/*
Store data somewhere on disk
*/

//Later you can decode it like this
let decoded_data = try NSKeyedUnarchiver.unarchivedObject(ofClass: PHPersistentChangeToken.self, from: data)
  • Awesome, thanks! Would it be possible to store this encoded data as JSON?

Add a Comment

I would not recommend attempting to try to serialize PHPersistentChangeFetchResult