Jetpack Compose LazyColumn&Retrofit:为详细视图提供数据的策略

huangapple go评论60阅读模式
英文:

Jetpack Compose LazyColumn & Retrofit: Strategy for providing data to a details-view

问题

当我获得一个包含项目(文章)的列表,这些数据是从Web API加载的,以JSON格式,使用Retrofit。
我使用加载的一些数据填充了一个LazyColumn。从单个文章可以导航到一个包含有关文章的所有数据的详细视图。

提供数据给详细视图的哪种方式更可取?

  • 当使用.navigate()时,将已加载的数据从列表视图传递到详细视图?
  • 还是只传递文章的ID到详细视图,然后使用ID通过Retrofit获取完整数据?
英文:

When I got a list with items (articles), which data is loaded from a web-API as JSON, using Retrofit.
I populate a LazyColumn with some of the loaded data. From a single article one can navigate into a details-view, which contains all data concerning the article.

What possibility for providing the data to the details-view is preferable?

  • Passing the already loaded data from the list-view to the details-view, when using .navigate()?
  • Or passing just the ID of the article to the details-view and then fetch the complete data with Retrofit, using the ID?

答案1

得分: 2

两种方法各有优点和考虑因素。首选方法取决于诸如数据更新的大小和频率、数据/信息类型以及要求等因素。如果数据很大,将其作为“bundle”在视图之间传递可能会导致内存使用增加,这是不推荐的。

处理这种情况的最佳方法是将一些初始信息传递给详细信息界面。当为详细信息进行API调用时,用户将在视图上看到初始信息。例如,在您的情况下,在文章详细信息页面上,您可以最初显示标题、日期、时间和标题图像作为基本信息。

但是,如果数据很大或动态,使用ID获取完整数据可以确保数据准确性并防止内存问题。通过在需要时检索完整数据,您可以为用户提供最新的信息同时最小化内存使用。

英文:

Both approaches have their own advantages and considerations. The preferable approach depends on factors such as the size and frequency of data updates, the type of data/information, and the requirements. If the data is large, passing it as a bundle between views can lead to increased memory usage, which is not recommended.

The best way to handle this situation is to pass some initial information to the details UI. When an API call is made for the details, the user will see the initial information on the view. For example, in your case, on the article details page, you can initially show the title, date, time, and header image as basic information.

However, if the data is large or dynamic, fetching the complete data using the ID can ensure data accuracy and prevent memory issues. By retrieving the complete data when needed, you can provide the user with the most up-to-date information while minimizing memory usage.

huangapple
  • 本文由 发表于 2023年5月28日 15:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定