Beware: ALAsset URLs are non-unique

iOS4 introduced the ALAssetLibrary, a way you can reference the files stored in the user’s camera roll, etc. Pretty useful, albiet a slightly convoluted API (what’s new?).

Here’s the catch: the ALAsset URLs returned are *not* guaranteed to be unique! When the user downloads & deletes photos from the camera roll (a very common use-case), that URL can potentially be re-used for another photo.

I discovered this in GPS Log, where I record the ALAsset URLs for all images taken (which can be quite a lot), and noticed that mis-matches were occurring.

My workaround is this:
In my list where I store the ALAsset URL, I now also store the ALAsset creationDate (accessible with [myAsset valueForProperty:ALAssetPropertyDate]). Thus when retrieving the asset, I first check that the retrieved creation date matches the one I am expecting. If it doesn’t then I treat the asset as missing, just like I do should the ALAssetLibrary not return any asset for that URL.

I’m afraid I can’t share my implementation as it’s not really all that portable – but I think you can get the gist of what I did from that description. Annoyingly, getting that creationDate is not so straight forward, as you can’t just execute it in the main thread (due to ALAssetLibrary’s design).

Why are these URLs reused I don’t know. I would prefer to see some UUID type setup. But anyway – be aware of this limitation, and prepare accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *