简体   繁体   English

在 viewDidLoad 中加载数据是否太晚而无法在 UIViewController 中显示?

[英]Is loading data in viewDidLoad too late to display it in a UIViewController?

Where do you usually load data when using a UIViewController?使用 UIViewController 时,您通常在哪里加载数据?

When I say "loading data" I mean: API calls to fetch it and its manipulation.当我说“加载数据”时,我的意思是:API 调用来获取它及其操作。

Do you load it in the view controller initializer, or load it before to initialize it and pass it?您是在视图controller初始化程序中加载它,还是在初始化它之前加载它并通过它? Or do you add the code to viewDidLoad?还是将代码添加到 viewDidLoad?

Also, what about the data for the rootViewController?另外,rootViewController 的数据呢? The one that initialises from storyboard when you launch the app?启动应用程序时从 storyboard 初始化的那个?

Do you load it in the view controller initializer, or load it before to initialize it and pass it?您是在视图controller初始化程序中加载它,还是在初始化它之前加载它并通过它? Or do you add the code to viewDidLoad?还是将代码添加到 viewDidLoad?

Usually this done inisde viewDidLoad ex in MVC you call the controller to load the data and refresh the contents of the vc like table/collection, regarding sending data case this known as dependency injection通常这是在 MVC 中完成的 inisde viewDidLoad ex,您调用 controller 来加载数据并刷新 vc 的内容,如表/集合,关于发送数据的情况,这称为依赖注入

Also, what about the data for the rootViewController?另外,rootViewController 的数据呢?

rootViewController is same as any other vc rootViewController与任何其他 vc 相同

First of all let me suggest not to have data loading code in ViewController if your app is not very very simple.首先,如果您的应用程序不是很简单,我建议不要在 ViewController 中使用数据加载代码。 It would be a better practice to have it in a separate class.最好将它放在单独的 class 中。 Check out MVP or VIPER design patterns for more info.查看 MVP 或 VIPER 设计模式以获取更多信息。

As for when to load your data (after view loaded or before) is more of a UX decision.至于何时加载数据(在视图加载之后或之前)更多的是用户体验决定。 If you start loading after, of course, screen might be empty for a while, it would be good to have some loading indicator on the screen so that users do not get confused.当然,如果你在加载之后开始加载,屏幕可能会暂时空着,最好在屏幕上有一些加载指示器,这样用户就不会感到困惑。 Whether users will see empty screens often or not depends on whether you store your loaded data permanently (in a local DB), have some run time cache, or non of that.用户是否会经常看到空白屏幕取决于您是否永久存储加载的数据(在本地数据库中),是否有一些运行时缓存,或者没有。 If you have some caching, empty screen will be visible just once, because if data is loaded and cached you will show that and update the screen after new data is received.如果你有一些缓存,空屏幕只会显示一次,因为如果数据被加载和缓存,你将显示它并在收到新数据后更新屏幕。

You can also start loading data before view is loaded (in loadView()) or may load some minimal data for the views which are not visible (so that they are not completely empty when opened) and then load full data after view is loaded.您还可以在加载视图之前(在 loadView() 中)开始加载数据,或者可以为不可见的视图加载一些最小数据(以便在打开时它们不完全为空),然后在加载视图后加载完整数据。

Any approach you choose is highly dependent on what kind of user experience do you want to provide, also on how long it takes to load your data.您选择的任何方法都高度依赖于您想要提供什么样的用户体验,以及加载数据需要多长时间。

Answer in one line is viewididload but below is the actual structural method.一行中的答案是 viewididload 但下面是实际的结构方法。

So for this you can have multiple answer the most popular answer for these days would be using MVC/MVVM.所以对于这个你可以有多个答案,这些天最流行的答案是使用 MVC/MVVM。 So let's discus about MVVM with delegate/protocol所以让我们讨论一下带有委托/协议的 MVVM

Model - your data. Model - 您的数据。

View model - here you initialize/modify/handle your data.查看 model - 在这里初始化/修改/处理您的数据。

Viewcontroller - viewdidload - here you can initialize your view model and interact with the data that can be provided by view model with the help of delegate-protocol mechanism. Viewcontroller - viewdidload - 在这里您可以初始化您的视图 model 并在委托协议机制的帮助下与视图 model 提供的数据进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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