Here are some thoughts on reusing a single copy of a viewController X:
Thread safety should have nothing to do with it as all user interface interactions need to be performed on the main thread.
Each instance of a viewController can only have at most one parent at a time. So as long as you only use X once in your view hierarchy, you should be OK. For example A presents B which presents X, and then X returns to B which returns to A. Then A presents C which presents X. This would work fine since X only has one parent at any given time.
Doing this would mean that X would retain all of its settings between presentations, so it would be the responsibility of the presenter to clean it up and properly initialize it before presentation. By keeping a permanent global copy of X around, it will retain all of the data it has between calls. For example, if there is an input text field, that text field will still contain the text from a previous use. The presenter will be responsible for reinitializing any data that viewController X retains and uses. This would include a delegate pointer.
An example of a scenario that would cause problems: A, B, and C are all contained in a UITabBarController
. Each one can present X
so that the user can change settings. This will fail if A presents X and then the user switches tabs to B and then presents X again because now X will have both A and B as parents, which can't work.
Instead of creating viewController NewIAPClass
as a global, consider making it a Singleton. See this answer: Creating a Singleton in Swift
So you'd create NewIAPClass
like this:
class NewIAPClass: UIViewController {
static let sharedInstance = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("IAP") as! NewIAPClass
// All of the other stuff that makes up NewIAPClass ...
}
Then every time you want to use NewIAPClass
, you'd refer to it as NewIAPClass.sharedInstance
.