I am attempting to write tests to check for retain cycles but came across this odd behavior. A UIViewController's properties do not get deallocated when setting the view controller to nil. Take this mock object for example:
class BasicViewController: UIViewController {
var someObject = NSObject()
.....
}
All it has is a variable. You would assume that when calling basicViewController = nil would cause someObject to be nil, but its not.
it("releases someObject") {
var controller: MockController? = MockController()
weak var something = controller?.something
expect(controller).toNot(beNil())
controller = nil
expect(controller).to(beNil())
expect(something).to(beNil())
}
it("doesn't release someObject") {
var controller: MockController? = MockController()
weak var something = controller?.something
expect(controller).toNot(beNil())
_ = controller?.view
controller = nil
expect(controller).to(beNil())
expect(something).toNot(beNil())
}
When calling vc.view this invokes loadView as well as the UIViewController's life cycle functions - viewDidLoad, viewDidAppear and viewWillAppear. My question is why? Why is it that when I reference a UIViewController's view property, that all objects the UIViewController owns persists, even after setting the UIViewController to nil.
FWIW, I am using Quick and Nimble for testing, as well as Swift 3.1
Short answer:
Add an autoreleasepool, which will dictate precisely when objects are drained from the pool, and it works as you would have expected.
Long answer:
I'm experiencing the same behavior that you describe. But the problem isn't the properties of the view controller. It's the view controller itself.
In your examples, you're setting controller to nil and appear using the fact that it is now nil to infer whether the controller has been deallocated or not. But that's only testing whether that particular reference to the view controller is nil, but the view controller itself might not yet be deallocated. But you can use your weak var test with the view controller itself. Consider this view controller:
class BasicViewController: UIViewController {
// this is intentionally blank
}
I can write tests where the view controller manifests the behavior that you describe, where that XCTAssertNil test after loading the view fails:
class MyApp2Tests: XCTestCase {
func testWithoutView() {
var controller: BasicViewController? = BasicViewController()
weak var weakController = controller
XCTAssertNotNil(weakController)
controller = nil
XCTAssertNil(weakController) // this succeeds
}
func testWithView() {
var controller: BasicViewController? = BasicViewController()
weak var weakController = controller
XCTAssertNotNil(weakController)
controller?.loadViewIfNeeded()
controller = nil
XCTAssertNil(weakController) // this fails
}
}
But when I added an autoreleasepool to explicitly control when the pool is drained, it worked as expected:
func testWithViewAndAutoreleasePool() {
weak var weakController: BasicViewController?
autoreleasepool {
var controller: BasicViewController? = BasicViewController()
weakController = controller
XCTAssertNotNil(weakController)
controller?.loadViewIfNeeded()
controller = nil
}
XCTAssertNil(weakController) // this succeeds
}
BTW, if you're looking for additional confirmation on the timing of the deallocation of the view controller, itself, add a print statement in deinit (as well as where you set controller = nil) and you'll see that the timing of deinit changes in the presence of doing anything that loads the view.
I can't explain this behavior. Why should doing something with the view affect the view controller lifecycle? BTW, I also performed the above tests with properties of the view controller, like in your question, and I see the exact same behavior (but IMHO, that's unsurprising because it's just because the view controller itself hasn't been deallocated).
At least we can explicitly control the autorelease pool lifecycle timing with autoreleasepool.
Related
I'm trying to keep a timer running even if I switch view controllers. I played around with the Singleton architecture, but I don't quite get it. Pushing a new view controller seems a little easier, but when I call the below method, the view controller that is pushed is blank (doesn't look like the view controller that I created in Storyboards). The timer view controller that I'm trying to push is also the second view controller, if that changes anything.
#objc func timerPressed() {
let timerVC = TimerViewController()
navigationController?.pushViewController(timerVC, animated: true)
}
You need to load it from storyboard
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCName") as! TimerViewController
self.navigationController?.pushViewController(timerVC, animated: true)
Not sure if your problem is that your controller is blank or that the timer resets. Anyway, in case that you want to keep the time in the memory and not deallocate upon navigating somewhere else I recommend you this.
Create some kind of Constants class which will have a shared param inside.
It could look like this:
class AppConstants {
static let shared = AppConstants()
var timer: Timer?
}
And do whatever you were doing with the timer here accessing it via the shared param.
AppConstants.shared.timer ...
There are different parts to your question. Sh_Khan told you what was wrong with the way you were loading your view controller (simply invoking a view controller’s init method does not load it’s view hierarchy. Typically you will define your view controller’s views in a storyboard, so you need to instantiate it from that storyboard.)
That doesn’t answer the question of how to manage a timer however. A singleton is a good way to go if you want your timer to be global instead of being tied to a particular view controller.
Post the code that you used to create your singleton and we can help you with that.
Edit: Updated to give the TimeManager a delegate:
The idea is pretty simple. Something like this:
protocol TimeManagerDelegate {
func timerDidFire()
}
class TimerManager {
static let sharedTimerManager = TimerManager()
weak var delegate: TimeManagerDelegate?
//methods/vars to manage a shared timer.
func handleTimer(timer: Timer) {
//Put your housekeeping code to manage the timer here
//Now tell our delegate (if any) that the timer has updated.
//Note the "optional chaining" syntax with the `?`. That means that
//If `delegate` == nil, it doesn't do anything.
delegate?.timerDidFire() //Send a message to the delegate, if there is one.
}
}
And then in your view controller:
//Declare that the view controller conforms to the TimeManagerDelegate protocol
class SomeViewController: UIViewController, TimeManagerDelegate {
//This is the function that gets called on the current delegate
func timerDidFire() {
//Update my clock label (or whatever I need to do in response to a timer update.)
}
override func viewWillAppear() {
super.viewWillAppear()
//Since this view controller is appearing, make it the TimeManager's delegate.
sharedTimerManager.delegate = self
}
Say I have view controllers A, B, C, D & E all embedded in a navigation controller. In view controller B, I have a custom UIImageView object. In C, I have a custom UITextfield object. Both custom classes have a reference to the view controller for various reasons such as I have to perform things like segue when a user taps the image view. To accomplish this, I have this inside each custom class file:
var controller: UIViewController?
And then inside each view controller, inside viewDidLoad I set that variable to self and everything works as expected (segues on tap etc..)
I have an unwind segue from E back to A. However, I noticed that due to these custom objects in view controllers B & C, both were not being deallocated due to a retain cycle caused by having this reference to the view controller. I fixed the issue by setting the controller variable to nil upon segue, however this creates a problem such that if the user goes back (pops the current view controller), because I set the controller variable to nil upon segue, nothing works (it wont segue again because controller var = nil). I thought I might fix this by adding viewWillAppear code as follows:
override func viewWillAppear(_ animated: Bool) {
usernameTextField.controller = self
passwordTextField.controller = self
}
Because I read that viewWillAppear will be called each time the viewcontroller comes into view. This did not fix the problem.
Any ideas on how to go about this? How can I set the controllers to nil during the unwind maybe...?
As the other answers have said you need to make it a weak reference like this:
weak var controller: UIViewControler?
However I would go further and say that you should not be keeping a reference to to a UIViewController inside any UIView based object (UIImageView, UITextField, etc). The UIViews should not need to know anything about their UIViewControllers.
Instead you should be using a delegation pattern. This is a basic example:
1) Create a protocol for the custom UIImageField like this:
protocol MyImageFieldProtocol: class {
func imageTapped()
}
2) Then add a delegate like this:
weak var delegate: MyImageFieldProtocol?
3) Your UIViewController then conforms to the protocol like this:
class MyViewController: UIViewController, MyImageFieldProtocol {
}
4) Somewhere inside the view controller (viewDidLoad is usually a good place you assign the view controller to the image views delegate like this:
func viewDidLoad {
super.viewDidLoad()
myImageView.delegate = self
}
5) Then add the function to respond to the protocol action to the view controller like this:
func imageTapped {
self.performSegue(withIdentifier: "MySegue", sender: nil)
}
var controller: UIViewController? should be a weak reference. Like this:
weak var controller: UIViewController?
To know more about that read about Resolving Strong Reference Cycles Between Class Instances in Swift's documentation.
You should use weak references when you keep some ViewControllers
weak var controller: UIviewControler?
You should check everything link to retain cycle, and referencing in swift :
https://krakendev.io/blog/weak-and-unowned-references-in-swift
https://medium.com/#chris_dus/strong-weak-unowned-reference-counting-in-swift-5813fa454f30
I had similar issues, I advice you to look at those link : How can I manage and free memory through ViewControllers
I ran into a bizarre bug earlier this week and wanted to follow up to see how to prevent the root cause of the issue.
Take the following code.
//*****************************
//MAINVIEWCONTROLLER CLASS CODE
//*****************************
//Some event happens that triggers me to want to load up TestViewController.
func showViewController(){
var testController = TestViewController()
testController.someMethod("Test1")
self.navigationController?.pushViewController(testController, animated: true)
}
//*****************************
//TESTVIEWCONTROLLER CLASS CODE
//*****************************
testView:TestView!
override func viewDidLoad(){
super.viewDidLoad()
testView = TestView()
...
}
func someMethod(someData:String){
testView.name = someData //AppCrashes here because testView might be nil.
...
}
So someMethod is getting fired before TestViewController has had the chance to go through and create the testView. I'm then getting a cannot unwrap an optional value because testView is nil and I'm accessing a property on it.
Whats strange is the application I'm running probably does this exact thing in 6 different places, and 5/6 are working perfectly fine, but 1/6 is now giving me this error. I'm guessing its because of the viewDidLoad not being guaranteed to fire immediately or complete before someMethod is executed, but why then is this not happening on all 6 of the use cases.
So my main questions are:
Why does this crash happen?
What is the best practice to avoid it.
Thanks! Thoughtful answers will get up-votes as always! Let me know if any more info would be helpful.
Basically never run code in the destination controller called from the source controller which involves UI elements. Create a property, set it in the source controller and assign the value to the UI element in viewDidLoad() of the destination controller, for example:
//*****************************
//MAINVIEWCONTROLLER CLASS CODE
//*****************************
//Some event happens that triggers me to want to load up TestViewController.
func showViewController(){
var testController = TestViewController()
testController.someData = "Test1"
self.navigationController?.pushViewController(testController, animated: true)
}
//*****************************
//TESTVIEWCONTROLLER CLASS CODE
//*****************************
testView:TestView!
var someData = ""
override func viewDidLoad(){
super.viewDidLoad()
testView = TestView()
testView.name = someData
...
}
viewDidLoad is called when the ViewController completes loading in preparation to be shown, e.g. when a segue takes place or when involved in a present.
As written your code shouldn't even compile since testView is optional, but you have two options. Use optionals (in which case the view may not get the information if not called after viewDidLoad, but it won't crash) or store the passed information and update your view in viewDidLoad or viewWillAppear.
Something you might want to be aware of is viewIfLoaded
You can force the viewDidLoad method with with:
var testViewController = TestViewController()
_ = testViewController.view
testViewController.someMethod("Test")
Initializing the ViewController doesn't automatically call viewDidLoad
I am working on an open source tutorial using MVVM, Coordinators and RxSwift. I am constructing all the viewcontrollers and models in the coordinator. Controller has a strong reference to viewmodel and when a viewmodel is set, I would like to perform some UI related actions(using property observer didSet). The problem I am facing is that didSet is called before viewDidLoad causing a crash.
Stripped down version of ViewController:
class MessageVC: UIViewController {
var viewModel: MessageViewModel! {
didSet {
manipulateUI() // crashes
}
}
override func viewDidLoad() {
super.viewDidLoad()
manipulateUI() // works fine if setup is correct in coordinator
}
Coordinator stripped down version:
extension AppCoordinator {
convenience init() {
let rootVC = MessageVC() // actual construction from storyboard
let messages = Message.getMessages()
rootVC.viewModel = MessageViewModel(withMessage: messages)
}
My concern is that even though calling manipulateUI in viewDidLoad is working for me currently, the app will crash if I forget to set the viewModel from my co-ordinator making me think that I am using a fragile architecture. I really like updating userinterface from didSet but it is called before viewDidLoad.
I know it is a simple problem but from architecture standpoint it seems fragile. Any suggestions, improvements and comments are appreciated a lot.
I wont say that cases like this can define wether you are dealing with fragile architecture or not because view controllers has their own life cycle which differs a lot from other objects life cycle. Anyway you can easily avoid crashes here using different approaches. For example :
Approach 1:
Put a guard statement at the very beginning of your manipulateUI function so this function wont manipulate UI until both view is loaded and model is set. Then call this function on viewDidLoad method and when viewModel is set:
func manipulateUI(){
guard let viewModel = self.viewModel , isViewLoaded else {
return
}
//continue manipulation here
}
Approach 2:
Since you are not sure wether view is loaded when you set the model and don't know if views are initialized yet, you can access the views as optional properties in manipulateUI function:
func manipulateUI(){
self.someLabel?.text = self.viewModel.someText
//continue manipulation here
}
Approach 3:
Since you are using RxSwift you can always register an observer for view controller's isViewLoaded property and set the data source after you are sure that view is loaded
Crash happens because at this point
rootVC.viewModel = MessageViewModel(withMessage: messages)
view controller is not initialized.
It won't work the way you're trying to accomplish, you have to call manipulateUI() inside viewDidLoad.
I just experienced a strange behavior when I do the following steps,
In a view controller method, create a View controller instance (local instance).
Add the view as a subview to the view controller's view.
The view is displayed properly. The view has a button and when I click the button, it crashes with EXEC_BAD-ACCESS. After debugging a while, found that the view controller instance is release and button click event is not fired since the controller doesnot exist.
When I moved the declaration of the view controller to class level, it started working. I feel if a view controller's view is on the screen, shouldn't the view controller instance be retained.
Any thoughts?
Some code pointer.
class SomeViewController:UIViewController{
var workingVC:SomeVC?
func crashingMethod()
{
let vc:SomeVC = SomeVC(nibName:"SomeVC", bundle:NSBundle.mainBundle())
let delegate:AppDelegate = UIApplication.sharedApplication().delegate
let applWindow:UIWindow = delegate.window!
applWindow.addSubview(vc.view)
}
func workingMethod()
{
self.workingVC = SomeVC(nibName:"SomeVC", bundle:NSBundle.mainBundle())
let delegate:AppDelegate = UIApplication.sharedApplication().delegate
let applWindow:UIWindow = delegate.window!
applWindow.addSubview(self.workingVC!.view)
}
}
When you create object like this
let vc:SomeVC = SomeVC(nibName:"SomeVC", bundle:NSBundle.mainBundle())
object lifetime is equal to scope where it is created.
When scope ends, ARC release this object and all associated objects.
But, when you add it on class level, lifetime of this object equal to lifetime of owner(SomeViewController). So, it is released only when owner released.
Is it clear for you?
Also you can read about it in Memory Management Section in Apple Documentation