Invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary… WTF?

After the upgrade to iOS 5 and a code refactoring to make use of ARC, I was testing my app and receiving a strange error:

invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary

this because, I wasn’t aware of an ALAssetsLibrary‘s fact: (quote from Apple’s reference)

The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.

This line of text should be displayed inside a warning box to catch developers attention, in fact it is an essential point to understand to create a reliable application that access to device’s media library! The point is that we have to ensure that an instance of ALAssetsLibrary is persisted in memory until we make use of ALAssets retrieved with it. To ensure this, I added a static method to retrieve a shared instance of that class (more or less like a singleton):

+ (ALAssetsLibrary *)defaultAssetsLibrary {
	static dispatch_once_t pred = 0;
	static ALAssetsLibrary *library = nil;
	dispatch_once(&pred, ^{
		library = [[ALAssetsLibrary alloc] init];
	return library; 

So, I can refer to it using [MyAssetsManager defaultAssetsLibrary] through my classes and I can use threads (NSOperations an NSOperationQueues) without having to use “trick” like performSelectorOnMainThread.

  • Thanks for this Davide – works a treat. I too was confused by this sudden weird message – and was very happy to find your blog post. A big help!


  • Mithun Dhali

    Thanks a lot. Saved a lot of time for me.

  • Justin Dewoody

    So, do I just put this method in my .m file and call it with viewDidLoad? Or what? Please help. I’m having this same friggin’ issue!

  • @Justin: you don’t have to copy and paste the method. You have instead to understand the problem as I did and explained. The point is that if you want to access to ALAssets, you have to be sure that the instance of ALAssetsLibrary is still in memory (be sure you don’t release it before!). So, you can implement your own solution. In my app I created a dedicated class “AssetsManager” which exposes a persistent in-memory library instance, but you can for example declare and store it in the Application’s delegate or where you want! And you can access it when you need it!

  • I’ve been thinking about the ALAssetsLibrary as a singleton a lot the past few days, but I can’t get my mind around why Apple didn’t do it. Especially with the introduction of these ALAsset lifetime issues, a singleton seems to make so much more sense..

    Is it possible that the assets in the library do not get updated when you try to access an instance of ALAssetsLibrary that was created before taking a new photo?

  • Thanks for the explanation, fixed it in a jiffy as soon you explained what was wrong. Cheers!

  • Raj

    Thanks dude
    It’s help me too.. and saved my time…..

  • jsaiz

    Thanks for your Post! Really help us!

  • someids

    Thanks so much. Expert indeed

  • Thanks for your code sharing really save a lot of time thank you

  • Thank you very much……. It works for me

  • TienLe

    Nice! Thank you very much.

  • Andrei

    Thanks for sharing this.