簡體   English   中英

如何在片段重新創建中從 firebase 恢復已在 recyclerview 中提取的數據

[英]how to restore the data that has already been fetched in recyclerview from firebase in fragment recreation

我已經在回收站視圖中從實時數據庫中檢索了數據,但是在重新創建片段后,再次獲取相同的數據。 我想恢復已經獲取的數據,並在片段重新創建后將其設置為回收器視圖。

娛樂意義 銷毀所有事物並重新創建它 我假設您希望盡量減少對服務器的調用次數以減少使用和計費成本

最簡單的方法是為您的數據庫啟用有關該文檔的持久性並修改您的代碼以檢查數據是否存在於緩存中並獲取它如果不存在則從服務器獲取它

當您定期更新數據時,這容易但不好

所以我建議的解決方案是使用 Singleton 模式創建一個存儲庫 Class 以制作單個實例

並讓服務器調用它的構造函數來獲取你需要的所有數據

這樣,您每次打開應用程序時都會調用服務器,但是當我來娛樂時,您將使用存儲庫中的數據

示例我將假設您獲得一個字符串列表

      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() 之前確保先從服務器獲取數據;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM