繁体   English   中英

将数据从RecyclerView保存到Firestore(以及多个片段)的最有效方法

[英]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文档中。 我想很容易为所有项目构建一个数组,并保存TextViewcharsequence ,无论是否已对其进行修改。

但我认为,即使数量为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM