[英]Efficient way to fetch FireStore data into two fragments with a shared ViewModel
[英]Most efficient way of saving data from RecyclerView into Firestore (and with multiple fragments)
我在從FireStore動態加載的多個fragments
加載了RecyclerView
。
每個fragment
可能有20到30甚至50個項目。 每個項目都有一個TextView
,代表一個可以由用戶添加或扣除的整數。 每個項目均以0開頭。
我想將所有非0的項目(換句話說,已修改的項目)保存到新的firestore
文檔中。 我想很容易為所有項目構建一個數組,並保存TextView
的charsequence
,無論是否已對其進行修改。
但我認為,即使數量為0,也要再次保存所有項目,這會浪費設備和firestore
的內存。
正確的方法是什么? 我在SO上看到的大多數示例都顯示根據recyclerview
列表的大小創建一個數組,並根據列表位置將每個條目保存到數組編號。
我只想在單擊“提交”按鈕后才寫入FireStore
,而不是在每個TextChanged上寫入Firestore
我認為這在帶寬使用方面會更好。
但是,如果我錯了,請隨時糾正我。
一些代碼:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(getSnapshot(position));
holder.myCustomTextListener.updatePosition(holder.getAdapterPosition());
}
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//<shortened, removed various @bindview's...>
public MyCustomTextListener myCustomTextListener;
public ViewHolder(View itemView, MyCustomTextListener myCustomTextListener) {
super(itemView);
ButterKnife.bind(this, itemView);
this.myCustomTextListener = myCustomTextListener;
this.quantity.addTextChangedListener(myCustomTextListener);
}
public void bind(final DocumentSnapshot snapshot) {
AddOrderProductList orders = snapshot.toObject(AddOrderProductList.class);
//<shortened, various setText's here...>
}
private class MyCustomTextListener implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
Log.d(TAG, "onTextChanged at " + position + " with this CharSequence: " + charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
Log.d(TAG, "afterTextChanged at " + position);
}
}
由於可能存在大量數據,我決定將本地條目保存到本地數據庫(SQLite)中。 它可能比Arrays或hashmap慢,但是相差微秒。 如果我要處理數百個條目,那么由於內存使用而導致的速度變慢了。
然后,我可以使用SQL查詢來獲取數量= 0的位置以上傳到Firestore。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.