I am initializing an object and then configuring it inline. But instead of the previous instance (if previously allocated) being deallocated before configuring it (on the directly following lines), the deallocation is being deferred. So all the configuring I do to it ends up being a deallocated object - which of course is not what I want.
For instance, if I run this code:
if paintingView != nil {
paintingView.removeFromSuperview()
}
print("^^^^^^^^^^^^^ About to init")
paintingView = PaintingView(frame: TDTDeviceUtilites.screenFrame())
print("^^^^^^^^^^^^^ About to add view")
view.addSubview(paintingView)
print("^^^^^^^^^^^^^ About to configure")
self.configurePaintingView()
I will see the following messages in the Console:
^^^^^^^^^^^^^ About to init
^^^^^^^^^^^^^ About to add view
^^^^^^^^^^^^^ About to configure
***** Painting View Dealloc ***** //message from the dealloc method of the PaintingView class.
I would expect the dealloc to happen directly following the init line. What is going on? Note that this code is being called in NSOperationQueue().
How can I make sure the dealloc happens first, and why isn't that already the case?