Search code examples
iosiphonecocoansstringuikit

Calculating string sizes on iPhone on a background thread


I've got some somewhat hefty string size calculations happening in my app (each one takes close to 500ms, and happens when the user scrolls to a new "page" in my app (like the Weather app). The delay only happens once per page, as the calculation only needs to be run once (and can even be cached for subsequent launches with the same data).

Anyway, I still like to not block the UI for this type of work, as to me it screams using threads, but I know UIKit is not meant to be used from other threads. (I know NSString is not part of UIKit, but the string sizing methods are part of the UIKitAdditions...)

So how should I go about doing this? What's the best way to not block the UI and do so safely?


Solution

  • Consider using NSOperation/NSOperationQueue. There's a tutorial on Cocoa Is My Girlfriend, and Apple has a guide.