简体   繁体   中英

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.

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.

I would suggest you to create a view controller which is responsible to show the loading of the application and in background it syncs with your backend. Having such a view controller along with loading indicator you can also show percentage progress for better UX. Once the sync is complete you can navigate to home controller.

Secondly, If you are loading the same data again with another launch. add a sync check so as not the same data is downloaded again and again preserving app's time and space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM