Monthly Archives: May 2011

ALAssetsLibrary and threads

I’m working on an iOS app right now with a feature that uses images from the Photo Library. This was all solid for me, and I had worked with it for a nearly a month before putting it before my alpha testers.

With a set up like that, you know where this is going: It totally did not work for them. At all. After the users would pick a photo from the library, the spinner letting the user know an image was being loaded would sit there forever, and eventually, this would show up in the console logs:

May 19 14:51:17 THE-MOON SpringBoard[27] : MultitouchHID(1ed4d440) uilock state: 0 -> 1

May 19 14:52:00 THE-MOON SpringBoard[27] : jotunheim[725] has active assertions beyond permitted time:
identifier: CoreLocationRegistration process: jotunheim[725] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:725 preventSuspend preventIdleSleep ,
identifier: CoreLocationRegistration process: jotunheim[725] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:725 preventSuspend preventIdleSleep

May 19 14:52:00 THE-MOON SpringBoard[27] : Forcing crash report of jotunheim[725]...

For the life of me, I could not reproduce this bug on my phone or my girlfriend’s phone. Which, of course, is bewildering. Googling pointed to a lot of problems related to threading, and indeed I was using a dispatch queue of my own making to do the image work.

I know there’s things that absolutely must be started on the main thread: Network calls (which end up on the web thread) and UI stuff. But I wasn’t doing anything with the network or the UI, as far as I knew. And why would this only happen on my users’ devices and not on devices in my household?
I’ll spare you a recounting the red herrings that I surveyed.
It’s because the first time you try to get stuff out of the Photo Library with ALAssetsLibrary, it asks the user if your app can have access to location data. (Photo metadata can contain with location data.) But it can’t show a UIAlertView from a thread other than the main thread, it can’t, so things will just stall out.

My phone and my lady friend’s phone have had on them previous builds of the app that used ALAssetsLibrary from the main thread. So, back then, that dialog was able to show, and location data access permission was saved. Deleting the app doesn’t revoke that permission. The current build, which used ALAssetsLibrary from a non-main thread, ran into no problems because it had the permission and didn’t need to show any dialogs.
The lessons I can see are:

1. Doing work in helper queues is great, but think twice about whether or not the things you do there are going to lead to UI or network stuff.

If I had read carefully, I would have noted that the documentation says:

When the asset is requested, the user may be asked to confirm the application’s access to the library. If the user denies access to the application, or if no application is allowed to access the data, the failure block is called.

2. Things that affect your app get saved outside of your app and don’t get cleared when you delete your app.

I hope this saves someone somewhere some time.

Parade raining: When to do it?

I was listening to this podcast in which the hosts, Hannah and Edward, discuss how to approach people’s enthusiasm about the long-awaited killing of Osama bin Laden.

The death of Osama bin Laden means quite a bit symbolically, but I don’t think that is going to translate to that much real-world effect, and it sounds like Edward and Hannah don’t think it will, either. If you ascribe to this point of view, it means that a lot of people are confusing the way in which this event is significant.

The question they ponder is: Should people that don’t really think this is going to change much just go along with the people that think this defines the start of a new era? Maybe this is a case in which the enthusiasm and positivity is more important that the reality of the situation, Edward ponders.

I think it’s trouble when enthusiasm is built on a false premise, like I think Hannah was saying. Man, just writing that last sentence felt totally wet blanketesque and possibly sanctimonious! But you gotta be willing to risk sanctimony in order to make sure as many of us as possible are focused on the real deal.

I do agree with them in that there’s no gain in smacking people down for feeling good. You cannot deny the honesty of their reactions, even if you disagree with the conclusions that spring from them. So, it’s a pretty fine line to walk, what with the keeping people you know connected to reality while also not being a shit. I think I’ve settled on, “I am glad you have achieved closure at last! However, feeling closure doesn’t necessarily mean the world is significantly better.” Which, again sounds a bit sanctimonious! I’ll have to work on that.

But better to be sanctimonious than to tacitly help build a false reality from which bad decisions are made. Many of our biggest problems today grew this way. And I know that there’s a lot of people that are mostly interested in believing in whatever what makes them feel good. It is still worth trying, though. Even if it makes you look like a shit.