Android 将数据从RecyclerView保存到Firestore(并使用多个片段)的最有效方法
我在多个Android 将数据从RecyclerView保存到Firestore(并使用多个片段)的最有效方法,android,firebase,android-recyclerview,google-cloud-firestore,Android,Firebase,Android Recyclerview,Google Cloud Firestore,我在多个片段上有一个RecyclerView加载了所有从fireStore动态加载的片段 每个片段可能有20到30个甚至50个项目。每个项目都有一个TextView,表示用户可以添加或减去的整数。每个项目都以0开头 我想将所有非0的项目(即已修改的项目)保存到新的firestore文档中。我想为所有项目构建一个数组并保存TextView的charsequence是很容易的,不管它是否被修改过 但我认为,即使数量为0,在设备和firestore中重新保存每件物品也是浪费内存 正确的方法是什么?我在
片段上有一个RecyclerView
加载了所有从fireStore动态加载的片段
每个片段可能有20到30个甚至50个项目。每个项目都有一个TextView
,表示用户可以添加或减去的整数。每个项目都以0开头
我想将所有非0的项目(即已修改的项目)保存到新的firestore
文档中。我想为所有项目构建一个数组并保存TextView
的charsequence
是很容易的,不管它是否被修改过
但我认为,即使数量为0,在设备和firestore
中重新保存每件物品也是浪费内存
正确的方法是什么?我在SO上看到的大多数示例都显示了根据recyclerview
列表的大小创建一个数组,并根据列表位置将每个条目保存到数组编号
我只想在点击“提交”按钮后才向FireStore
写入内容,而不是在每次文本更改时向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);
}
}
@覆盖
公共无效onBindViewHolder(ViewHolder,int位置){
持有者绑定(获取快照(位置));
holder.myCustomTextListener.updatePosition(holder.getAdapterPosition());
}
静态类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
//
}
私有类MyCustomTextListener实现TextWatcher{
私人职位;
公共无效更新位置(整数位置){
这个位置=位置;
}
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i2,int i3){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i2,int i3){
Log.d(标签,“onTextChanged at”+位置+”,带有此字符序列:“+CharSequence”);
}
@凌驾
public void PostTextChanged(可编辑){
Log.d(标签,“后拉伸”在“+位置改变);
}
}
由于潜在的大量数据,我决定将本地条目保存到本地数据库(SQLite)中。它可能比数组或hashmap慢,但相差微秒。如果我要处理数百个条目,那么由于内存使用而导致的速度减慢会更糟
然后我可以使用SQL查询来获取where quantity!=0上载到firestore。为每个项目创建对象将占用非常高的内存。对于每一个项目也要有一个arraylist,可能会比对象使用更少的内存,但效率不高。我脑海中浮现出一些想法:在onTextChanged
上创建并销毁对象-如果是0,则销毁对象。如果其>0,则是否创建对象?或者一个ArrayList实现,其中if>0,然后在它为0时删除?