When a UIViewController that does not override viewDidLoad() registers a @ViewModel property, the UIViewController implementation of viewDidLoad() is swizzled. When an unrelated class that does override viewDidLoad() registers a @ViewModel, it now has multiple swizzled implementations in its class hierarchy. The result is UIViewController.viewDidLoadNotification being posted multiple times for the same invocation of viewDidLoad() on a UIViewController subclass, once for each implementation of viewDidLoad() in the call stack.
Use an associated object to keep track of whether a viewDidLoad() implementation earlier in the stack is already planning to post the notification, so that recursive calls can check this property and skip posting duplicate notifications.
This maintains the desired property that UIViewController.viewDidLoadNotification is posted only once, after the most-derived viewDidLoad() implementation returns. It should guarantee that a given ViewModelObserver class has loaded all the views it knows about before accessing them in updateView().
When a
UIViewController
that does not overrideviewDidLoad()
registers a@ViewModel
property, theUIViewController
implementation ofviewDidLoad()
is swizzled. When an unrelated class that does overrideviewDidLoad()
registers a@ViewModel
, it now has multiple swizzled implementations in its class hierarchy. The result isUIViewController.viewDidLoadNotification
being posted multiple times for the same invocation ofviewDidLoad()
on aUIViewController
subclass, once for each implementation ofviewDidLoad()
in the call stack.Use an associated object to keep track of whether a
viewDidLoad()
implementation earlier in the stack is already planning to post the notification, so that recursive calls can check this property and skip posting duplicate notifications.This maintains the desired property that
UIViewController.viewDidLoadNotification
is posted only once, after the most-derivedviewDidLoad()
implementation returns. It should guarantee that a givenViewModelObserver
class has loaded all the views it knows about before accessing them inupdateView()
.