iOS/Swift: in which function between viewDidLoad and viewWillAppear am I supposed to query a database?

前提是你 提交于 2020-01-05 01:01:35

问题


In my app, being developed by Swift and XCode 6, I have to query a database and according to that answer then I'll modify and show some UI widgets which, in my case, are three colored buttons whose I have to change their text which consists in a number. These buttons indicate the number of tasks assigned to an user and they have different colors according to the tasks' priority.

So, shall I query the database in viewDidLoad() function and then change the buttons' text in viewWillAppear according to the answer? Is that right?


回答1:


You can query a database whenever you want.

ViewDidLoad will only get called once per instance of your view controller. It is typically used for initializing objects.

In view[Will/Did]Appear you typically update your UI to reflect the newest data you have by setting label text and image view images.

Your described approach is correct if you don't need to re-query the database every time a view appears.




回答2:


yes you can query it in viewDidLoad but if you do it in viewWillAppear will be more good because viewDidLoad calls once when view loading first time and never called again until unless view will deloc but viewWillAppear call every time for example you have one view and it have navigation view inside and you navigate inside the other view so when you press back button viewDidLoad will not call but yes viewWillAppear will always call.




回答3:


Yes you can query in viewDidLoad and do UI modifications in viewDidAppear.

Actually it depends on your need. If your result will change every time OR if you wanna refresh data every time your View's display then query and modify UI in viewWillAppear OR viewDidAppear as viewDidLoad will query only once at the time your View first loads.



来源:https://stackoverflow.com/questions/32286330/ios-swift-in-which-function-between-viewdidload-and-viewwillappear-am-i-suppose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!