Преглед изворни кода

[WIP] Fixing memory leak in ChatListController (#1699)

* Ensure that tabbar root controller always call willMove before they get replaced during account switch. Fixes a memory leak in ChatListController

* say why calling willMove is needed

Co-authored-by: B. Petersen <r10s@b44t.com>
cyBerta пре 2 година
родитељ
комит
1cf1b4013d
1 измењених фајлова са 11 додато и 0 уклоњено
  1. 11 0
      deltachat-ios/Coordinator/AppCoordinator.swift

+ 11 - 0
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -235,6 +235,17 @@ class AppCoordinator {
     }
 
     func resetTabBarRootViewControllers() {
+        // call `willMove()` for the root view controllers of each tab, after popping to root.
+        // this is not always done by `setViewControllers()` the documentation is vague on this point:
+        // <https://developer.apple.com/documentation/uikit/uitabbarcontroller/1621177-setviewcontrollers>
+        // (calling `willMove()` is needed eg. to remove observers - otherwise we have a memory leak)
+        self.tabBarController.viewControllers?.forEach { controller in
+            if let navController = controller as? UINavigationController {
+                navController.popToRootViewController(animated: false)
+                navController.viewControllers[0].willMove(toParent: nil)
+            }
+        }
+
         self.tabBarController.setViewControllers([createQrNavigationController(),
                                                   createChatsNavigationController(),
                                                   createSettingsNavigationController()], animated: false)