如何恢复已经在recyclerview中从firebase中获取的数据



我已经在回收者视图中从实时数据库中检索了数据,但在重建片段后,再次提取了相同的数据。我想恢复已经获取的数据,并在片段重建后将其设置为回收器视图。

娱乐意义破坏一切,再创造我想你想尽量减少对服务器的调用次数,以减少使用和计费成本

最简单的方法是对数据库启用持久性这方面的文件修改你的代码来检查数据是否存在于缓存中并获取它如果没有,从Server

获取当你定期更新数据时,这很容易但不好

所以我建议的解决方案是创建一个具有单例模式的存储库类来创建单个实例

并让服务器调用它的构造函数来获取你需要的所有数据

这样每次应用打开时,你都会调用一次服务器但当我来娱乐时,你会使用存储库

中的数据激发我将假设你得到一个字符串列表

public class Repository {
private static Repository instance = null;
private static final Object LOCK = new Object();
private List<String> strings;

private Repository (){
strings = {Make Server Call Here}
}

public static Repository getInstance(){
if (instance == null){
synchronized (LOCK){
if (instance == null){
instance = new Subjects_Repository();
}
}
}
return instance;
}
public List<String> getStrings(){
return strings;
}

注意:从firebase获取数据在后台线程完成,所以确保在使用getStrings()方法之前先从服务器获取数据;

最新更新