简体   繁体   English

如何在应用启动时预加载Firestore数据?

[英]How to preload firestore data on app start up?

I am building an app that tracks the location of nearby coffee shops and allows users to view each shop's available products. 我正在构建一个跟踪附近咖啡店位置的应用程序,并允许用户查看每家咖啡店的可用产品。 I am using firestore to store all of the data for each product and showcasing this data to the user through a recyclerview + firestoreRecyclerAdapter. 我正在使用Firestore存储每个产品的所有数据,并通过recyclerview + firestoreRecyclerAdapter向用户展示此数据。 It works. 有用。 However, the product data loads really slowly. 但是,产品数据的加载速度非常慢。 Taking several seconds or even minutes to load the recyclerview. 花费几秒钟甚至几分钟来加载recyclerview。 Even after the recycler is fully loaded, this problem persists. 即使在回收站完全加载后,此问题仍然存在。

Now, I think that it's because he reads from the database are overloading the main UI thread. 现在,我认为这是因为他从数据库读取的内容正在使主UI线程超载。 So I have tried to perform these reads in an asyncTask, but I can't pull values from the asyncTask since it outputs data after the activity has requested it. 因此,我尝试在asyncTask中执行这些读取操作,但是我无法从asyncTask中提取值,因为它在活动请求后会输出数据。 Giving me an error. 给我一个错误。

I have tried decreasing the amount of data that is pulled from firestore. 我尝试减少从Firestore提取的数据量。 But that doesn't help either. 但这也无济于事。

I am thinking about trying to load all of the product data from firestore to shared preferences, internal storage, or a package of some kind while the app is starting up. 我正在考虑尝试在启动应用程序时将所有产品数据从Firestore加载到共享首选项,内部存储或某种程序包。 That way requests don't need to be sent to the server. 这样,不需要将请求发送到服务器。 In this way, the server can serve as a way to update this data rather than continuously attempt to retrieve it. 这样,服务器可以用作更新此数据的方式,而不是连续尝试检索它的方式。

However, I am lost on how to do this. 但是,我不知道该怎么做。 How can a load firestore data say a custom object, save that list of custom objects to an array, then provide some way for this data to be loaded in and accessed later? 加载Firestore数据如何说一个自定义对象,然后将该自定义对象列表保存到数组中,然后提供某种方式供此数据加载并稍后访问?

Firestore database has offline persistence enabled for mobile platforms by default. Firestore数据库默认情况下为移动平台启用了脱机持久性。 It means it saves loaded data and what you have queried in cache. 这意味着它将保存加载的数据以及您在缓存中查询的内容。 Try to read this https://firebase.google.com/docs/firestore/manage-data/enable-offline . 尝试阅读此https://firebase.google.com/docs/firestore/manage-data/enable-offline Also do not try to save your data in shared preferences, it is designed to contain small chunks of data stored in Key/Value pair. 也不要尝试将数据保存在共享首选项中,它被设计为包含存储在键/值对中的小块数据。 To be more specific, I think the performance problem is in your code. 更具体地说,我认为性能问题出在您的代码中。

I would suggest few things: move realization of your code to your activity/fragment - from your adapter.(Just create interfaces with which you can operate to open new activity/fragment/onClick something etc with your items in adapter) 我会建议一些事情:将代码的实现从适配器转移到活动/片段(只需创建接口,您就可以使用这些接口在适配器中的项目上打开新的活动/片段/ onClick等)

I also highly suggest to use Livedata + ViewModel, it helped me a lot to deal with performance problems. 我也强烈建议使用Livedata + ViewModel,它对解决性能问题有很大帮助。 Check out this blog https://firebase.googleblog.com/2017/12/using-android-architecture-components.html . 查看此博客https://firebase.googleblog.com/2017/12/using-android-architecture-components.html It helped me to understand the principles and implement it in my code. 它帮助我理解了原理并在代码中实现了它。 Good Luck! 祝好运!

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

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