App Crashes After Canceling Print Activity

793 views Asked by At

My app supports printing. If the user cancels the Printer Options modal view controller, the app crashes somewhere in Apple's code with a message of _WebTryThreadLock(bool) ... Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

I've narrowed it down to the following code:

let itemProvider = UIActivityItemProvider(placeholderItem: "message")
let activityItems = [ itemProvider,
                      UIMarkupTextPrintFormatter(markupText: "test") ]
let activityController = UIActivityViewController(activityItems: activityItems,
                                              applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)

If I remove itemProvider from the activityItems array, the crash goes away. In my app I have a custom subclass of UIActivityItemProvider, but even if I use the superclass without customizing it, I get this crash.

To replicate, create a single-view project with a single button, and link the button to an action that uses the code snippet above as its body. Then tap the button, tap the Print icon in the activity view controller, and then tap the Cancel button in the Printer Options view controller. Crash. Any ideas?

1

There are 1 answers

0
Jonge On BEST ANSWER

It is almost certainly a bug in Apple's code.

After debugging, I came to the conclusion that it is related to deallocation of the UIMarkupTextPrintFormatter instance.

So as a workaround, you can have the formatter as a property in your class:

var printFormatter = UIMarkupTextPrintFormatter(markupText: "test")

Then use it like this:

let itemProvider = UIActivityItemProvider(placeholderItem: "message")
let activityItems = [itemProvider, self.printFormatter]
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)

As long as the property is held strongly by your instance and is not deallocated, it won't crash.