[英]Most efficient way of saving data from RecyclerView into Firestore (and with multiple fragments)
I have a RecyclerView
on multiple fragments
loaded all dynamically loaded from fireStore. 我在从FireStore动态加载的多个fragments
加载了RecyclerView
。
Each fragment
may have 20 to 30 or even 50 items. 每个fragment
可能有20到30甚至50个项目。 Each item have a TextView
that represent an integer that can be added or deducted by the user. 每个项目都有一个TextView
,代表一个可以由用户添加或扣除的整数。 Each item start with a 0. 每个项目均以0开头。
I would like to save all items that is not 0 (in other words, items that have been modified) into a new firestore
document. 我想将所有非0的项目(换句话说,已修改的项目)保存到新的firestore
文档中。 I guess it is easy to build an array for all the items and save the charsequence
of the TextView
whether or not it has been modified. 我想很容易为所有项目构建一个数组,并保存TextView
的charsequence
,无论是否已对其进行修改。
But I think it is a waste of memory on the device and in firestore
to save every item all over again even if the quantity is 0. 但我认为,即使数量为0,也要再次保存所有项目,这会浪费设备和firestore
的内存。
What is the proper way to do this? 正确的方法是什么? Most examples I seen on SO shows to create an array according to the size of the recyclerview
list, and save each entry according to list position to the array number. 我在SO上看到的大多数示例都显示根据recyclerview
列表的大小创建一个数组,并根据列表位置将每个条目保存到数组编号。
I would like to write to FireStore
only after "Submit" button is clicked, instead of writing to Firestore
on each TextChanged - I think that would be better implementation in terms of bandwidth usage. 我只想在单击“提交”按钮后才写入FireStore
,而不是在每个TextChanged上写入Firestore
我认为这在带宽使用方面会更好。
But feel free to correct me if I'm wrong. 但是,如果我错了,请随时纠正我。
Some codes: 一些代码:
@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);
}
}
I have decided to save local entries into local database (SQLite) because of the potential large amount of data. 由于可能存在大量数据,我决定将本地条目保存到本地数据库(SQLite)中。 It may be slower than Arrays or hashmap but that's in microsecond difference. 它可能比Arrays或hashmap慢,但是相差微秒。 If I will be dealing with hundred of entries, then the slow down due to memory usage will be worse. 如果我要处理数百个条目,那么由于内存使用而导致的速度变慢了。
And then I can use SQL query to get where quantity != 0 to upload to firestore. 然后,我可以使用SQL查询来获取数量= 0的位置以上传到Firestore。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.