# Interview Questions - Life Cycle ### 1. 試說明 App Life Cycle - App回應系統通知、使用者互動、處理其他系統相關事件的不同狀態,像是unattached, foreground, background, suspended。app的狀態會決定可以做什麼跟不能做什麼 - iOS13之後,改成 scene-based app,用 `UISceneDelegate`來處理生命週期相關的事 - ![](https://i.imgur.com/7vaq1fG.png) - When the user or system requests a new scene for your app, UIKit creates it and puts it in the unattached state. - User-requested scenes move quickly to the foreground, where they appear onscreen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. - When the user dismisses your app's UI, UIKit moves the associated scene to the background state and eventually to the suspended state. UIKit can disconnect a background or suspended scene at any time to reclaim its resources, returning that scene to the unattached state. - iOS12之前是用 ` UIApplicationDelegate`來處理生命週期相關的事 - (iOS12之前)AppDelegate 是一個 App 中的核心,它可處理初始化、各種生命週期狀態間的轉換、甚至是一些較為複雜的事件 - ![](https://i.imgur.com/rVp6pUJ.png) - After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear onscreen. - When launching to the foreground, the system transitions the app to the active state automatically. - After that, the state fluctuates between active and background until the app terminates. [混淆系列:App 的生命週期「App Life Cycle」](https://medium.com/%E5%9C%A8%E7%A8%8B%E5%BC%8F%E8%88%87%E6%97%85%E8%A1%8C%E7%9A%84%E8%B7%AF%E4%B8%8A/%E6%B7%B7%E6%B7%86%E7%B3%BB%E5%88%97-app-%E7%9A%84%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-app-life-cycle-6ef9c88e9737) ### 2. 試說明 View Controller Life Cycle。 - VC 是app的地基,負責user interfaces和 underlying data的溝通,View Controller Life Cycle就是指VC的生命週期,從產生到消失 - ![](https://i.imgur.com/VnShP0K.png) - 還沒輪到該VC時,都是處於 Not Loaded狀態,而一旦被呼叫時就會開始進入生命週期 - loadView() - 而當view被需要(被呼叫),但是他還是nil時,viewController就會呼叫loadView(),生成UIView然後assign到 - viewDidLoad() - Called after the controller’s view is loaded into memory. — by APPLE 在畫面已經載入到記憶體的時候就會呼叫這個方法,並呈現給使用者看 - viewWillAppear() - Called after the title bar accessory view controller’s view has been loaded into memory is about to be added to the view hierarchy in the window. — by APPLE 畫面載入記憶體且即將要把畫面加入到呼叫階層(hierarchy)中呈現給使用者時 - 當我們的畫面會一直更新、或是即將呈現給使用者時,就可以使用該方法 - 使用情境: - 網路連線如果需要一直更動的話,讓它在呈現給使用者前就會先載入好 - 一些動畫等。 - 在畫面即將被呈現在使用者前,可將一些元件隱藏、動作取消等。 - 其他的客製化任務 - viewDidAppear() - Called when the title bar accessory view controller’s view is fully transitioned onto the screen. — by APPLE - 當畫面剛呈現在螢幕上時,viewDidAppear 會被呼叫。這種方法很適合用在當畫面呈現後需要立刻做的事情 - 播放動畫、音樂、影片等。 - 獲取資料、儲存資料等 - viewWillDisappear() - Called when the view controller’s view is about to be removed from the view hierarchy in the window. — by APPLE當畫面即將在螢幕上被移除,但還保留在整個 View Controller 的呼叫階層(hierarchy)中,會呼叫該方法 - 可以做的事情: - 處理、控制 Timer。 - 隱藏、收回鍵盤。 - 終止網路的請求(request)。 - 有更改到的 UI 畫面回復至最初的基本 UI 畫面。如恢復顯示tab bar/naviBar - 也可以用於儲存 App狀態(state) - viewDidDisappear() - Called after the title bar accessory view controller’s view is removed from the window’s view hierarchy. — by APPLE當畫面從 view 的呼叫階層中移除後會呼叫該方法 - 可以做的事: - 完全停止接收通知(notification)。 - 裝置的感應偵測器(sensors)。 - 釋放資源。 - 其他需要被終止的活動。 - 其他較不常用的方法: - viewWillLayoutSubviews() - Called to notify the view controller that its view is about to layout its subviews. — by APPLE - 當我們的 bonds,畫面的邊界尺寸需要做更改時,在畫面還沒呈現出來前,可以呼叫此方法 - 可以用在: - 新增一個 subView。 - bond or frame 的變化。 - 屏幕水平放置 / 垂直時的更動。 - viewDidLayoutSubviews() - Called to notify the view controller that its view has just laid out its subviews. — by APPLE - 跟上面的方法相呼應。當畫面尺寸已經完成調整 / 變更會呼叫此方法。可以使用在: - 儘管畫面已經做好調整,但是你還是想要更動一些畫面尺寸時。(如旋轉屏幕) - 注意的點:它可以被多次呼叫,會在 Auto layout 完成後呼叫該方法。 - didReceiveMemoryWarning() - Sent to the view controller when the app receives a memory warning. — by APPLE - 我們的 App 不會主動呼叫這個方法,它是由系統發送給 View Controller 的。當你寫的 App 佔據大量的記憶體,造成效能不足時,系統會呼叫該方法。 - 假設當你的 App 滿載且負荷不了時,系統會自動關閉你的 App。這對於使用者來說,猶如閃退。但我們還是可以 Override 這個方法,讓 View Controller 去主動釋放一些不必要的記憶體,避免到時候系統直接關閉我們的 App。 [混淆系列:View Controller 的生命週期「View Controller Life Cycle」](https://medium.com/%E5%9C%A8%E7%A8%8B%E5%BC%8F%E8%88%87%E6%97%85%E8%A1%8C%E7%9A%84%E8%B7%AF%E4%B8%8A/%E6%B7%B7%E6%B7%86%E7%B3%BB%E5%88%97-view-controller-%E7%9A%84%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-view-controller-life-cycle-179462e95894) ### 3. 試說明 viewDidLoad 與 viewDidAppear 有什麼不同? - viewDidLoad() - Called after the controller’s view is loaded into memory. — by APPLE 在畫面已經載入到記憶體的時候就會呼叫這個方法,並呈現給使用者看 - 什麼時候用到: - 一次性的連線網路資料。 - 準備畫面,如背景色、元件樣子、其他使用者介面。 - 其他資料只需要被載入一次 - 要注意的: - 系統只會在啟動時呼叫 ==一次== viewDidLoad - viewDidLoad 會比「bond(邊界)」以及「rotation(旋轉)」先被呼叫,如果有需要調整畫面尺寸等方法的時候,需要留意是否要放在 viewDidLoad 裡,以免造成錯誤、閃退等 bug - viewDidAppear() - Called when the title bar accessory view controller’s view is fully transitioned onto the screen. — by APPLE - 當畫面剛呈現在螢幕上時,viewDidAppear 會被呼叫。這種方法很適合用在當畫面呈現後需要立刻做的事情 ### 4. 如何決定程式碼要寫在哪個 View Life Cycle 裡? Call API 的 code 要寫在哪? 隱藏UINavigationBar 的 code 要寫在哪? - 如何決定程式碼要寫在哪個 View Life Cycle 裡? - 執行的次數,只需要一次的話 -> viewDidLoad() - 執行的時間點,例如出現時、消失時,出現在畫面呈現之前、之後 - Call API 的 code 要寫在哪? - 一次的話是viewDidLoad() - 會需要一直更新的話,可以寫在viewWillAppear() - 隱藏UINavigationBar 的 code 要寫在哪? - viewWillAppear(),在呈現畫面之前 - viewWillDispear(),退出畫面之前 ### 5. 什麼時候可以確定 Auto Layout System 已經計算完畢,並已經把結果套到 View 上? * view設定 autolayout會經過 3個階段update, layout and render :::spoiler * These steps do not occur in a one-way direction. It’s possible for a step to trigger any previous one or even force the whole cycle to repeat. - update - 這個階段會根據各個view的constraints計算位置跟大小(This step is all about calculating view frame based on its constrains. ) - 系統會以bottom-up的方式開始遍歷 view hierarchy,例如從subview 到 superview,以及為每個view呼叫 `updateConstraints()` - 這個過程是自動發生的,但是我們也可以透過其他方式觸發 - `setNeedsUpdateConstraints()` 會讓目前的constraints失效,然後再下一個cycle更新constraints - `updateConstraintsIfNeeded()` 在constraints已失效的狀態下,會立刻觸發`updateConstraints()` 更新constraints - layout - 這階段會根據在Update階段計算的結果更新畫面layout(During this step, frames of each view are updated with the rectangles calculated in the Update phase ) - 系統會以top-down的方式開始遍歷 view hierarchy,例如從superview 到 subview,以及為每個view呼叫 `layoutSubviews()` - `setNeedsLayout()`會讓目前的layout失效,然後再下一個cycle更新layout - `layoutIfNeeded()`會在layout失效的狀態下,立即觸發`layoutSubviews()` - (基本上這三個方法跟update的三個方法意思很像,彼此間的關係一樣) - render - This step is responsible for bringing pixels onto the screen. - This step is independent of whether Auto Layout is enabled for a view or not. Same as with the layout step, rendering happens top-down - key method `drawRect` - About UIVC - Methods from steps 1 and 2 have their counterparts for the view controllers: - Update phase: updateViewConstraints. - Layout phase: viewWillLayoutSubviews / viewDidLayoutSubviews. ::: 在`viewDidLayoutSubviews` 的時候,會通知VC說 view已經結束 layout階段,就是已經根據在Update階段計算的結果,並更新結果到view上了,那後續view就可以再準備呈現到螢幕畫面上了 (Method viewDidLayoutSubviews is the most important among all. It is called to notify view controller that its view has finished the Layout step, i.e. it’s bounds have changed. This is an opportunity to make changes to a view after it has laid out its subviews, but before it becomes visible on the screen.) ### 6. 試說明 loadView 這個 function 有什麼用途,可以直接使用這個 method 嗎? - What Do in View Load : **loadView( ) basically takes a view (that you create) and sets it to the viewController’s view (superview).** 當(VC的)view被需要(被呼叫),但是他還是nil時,viewController就會呼叫loadView(),生成UIView然後assign到 - 不管是通过xib文件还是通过重写loadView方法创建UIViewController的view,在view创建完成之后都会调用viewDidLoad方法,也可以理解为当UIViewController的view被加载到記憶體中时,viewDidLoad方法会被调用 --> 可以用來控制進入 viewDidLoad()的時機 - You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property. - 蘋果文件:You should never call this method directly. The view controller calls this method when its [view](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property is requested but is currently `nil`. This method loads or creates a view and assigns it to the [view](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property. [iOS - loadView和viewDidLoad](https://blog.csdn.net/longshihua/article/details/79420686) ### 7. 當從 A Controller push B Controller,會經過哪些 view controller life cycle method? :crying_cat_face: 看A水的 ### 8. 當在不同的 tab 之間切換,會經過哪些 view controller life cycle method? :crying_cat_face: 看A水的 ### 9. 現在 A Controller 是畫面上的 view controller,其中有一個 Button,點擊之後會推出 B Controller。但是現在有個問題,點擊之後,經過了非常久的時間 B Controller 才出現在畫面上。如果要解決這個問題,你會怎麼開始? :crying_cat_face: [庭安筆記](https://crystal-zephyr-cb4.notion.site/Interview-Answers-Life-Cycle-e63629104e39495fafe8f4218f0f4464)