2

I'm implementing State Restoration in my app to allow the app to return to right where it was after the app has been purged by the OS.

Encoding and decoding the necessary variables (such as text the user has typed in a text field) is fairly straightforward, however, how do I save completion handler blocks set on a variable?

For instance, take Apple's UIAlertController. You create it with UIAlertActions, which when created have a parameter for a handler that fires when tapped. handler: ((UIAlertAction) -> Void)?

If I were Apple, and I wanted to restore the UIAlertController that was presented, how would I restore that handler? It obviously contains an unpredictable slew of variables being used in its scope, so I can't imaging you can just use NSCoder's encodeObject method.

Is there an accepted strategy for this? Or even a way to do it whatsoever?

Yes, I know delegation is an option, but blocks are being used more and more lately, and as above Apple is using them. The ability to capture the surrounding variable context is important and delegation doesn't provide.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

0 Answers0