Krumelur
Krumelur

Reputation: 33048

Is UImage.AsPNG() thread safe?

Is UImage.AsPNG() thread safe? When using UImage.AsPNG() in Xamarin.iOS 6.3.5.43 from a separate thread, I'm getting a MonoTouch.UIKit.UIKitThreadAccessException. From the Apple docs I cannot see that this method is required to be executed on the main thread. I did not get this warning in the 6.2 branch.

Upvotes: 2

Views: 74

Answers (2)

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19335

Yes, it is thread-safe and it has been fixed in the recently released Xamarin.iOS 6.2.7.

The problem was that we had a discussion with Apple engineers about thread-safe API, and we made a mistake when we went through the information they'd given us. A bit more information is available in the bug report.

Upvotes: 3

lxt
lxt

Reputation: 31294

From the Apple docs I cannot see that this method is required to be executed on the main thread

Apple don't specify per-method thread safety for UIKit. Rather, this disclaimer is given in the main framework reference:

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

Going back to your original problem - UIKitThreadAccessException is thrown on debug builds by MonoTouch. It's intended to serve as a checker/debug tool to make sure you're not doing UIKit stuff on a secondary thread.

Because from iOS 4 certain UIKit methods (mainly those involving drawing to a graphics context) became thread safe there's a whitelist of methods that MonoTouch won't throw this exception for, which is available in the MonoTouch documentation.

I couldn't say whether UIImage.AsPNG() is or isn't thread safe...I don't think it is, but if I'm wrong - or you want to risk it - you can pass the --disable-thread-check flag to disable MonoTouch's automatic UIKit background thread checking. This blog post might be helpful.

Upvotes: 1

Related Questions