iosswiftios-lifecycle

Where in my iOS (Swift) application lifecycle should i fetch all my data from firebase?


User may not interact with my app, until all data is fetched from firebase. So my newbie plan so far is:

  1. Start spinner
  2. Fetch data from firebase (async)
  3. When step 2 async job is completed, then hide spinner

My first thoughts as a newbie is often not the way to go and there must be a smarter way, where the framework is involved (maybe some place in app lifecycle) or a best practice pattern.
I can't find any useful stuff on www. A link can also be helpfull.


Solution

  • You should fetch your data in ViewDidLoad()

    viewDidLoad()—Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. The view controller’s outlets are guaranteed to have valid values by the time this method is called. Use this method to perform any additional setup required by your view controller.

    Typically, iOS calls viewDidLoad() only once, when its content view is first created; however, the content view is not necessarily created when the controller is first instantiated. Instead, it is lazily created the first time the system or any code access the controller’s view property.

    Your data will be fetched when you view in loaded.

    If you want to refresh your data whenever your view gets diplayed then i use may use.

    viewWillAppear()—Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen. Despite the name, just because the system calls this method, it does not guarantee that the content view will become visible. The view may be obscured by other views or hidden. This method simply indicates that the content view is about to be added to the app’s view hierarchy.