简体   繁体   中英

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.

Each fragment may have 20 to 30 or even 50 items. Each item have a TextView that represent an integer that can be added or deducted by the user. Each item start with a 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. 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.

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.

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.

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.

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. It may be slower than Arrays or hashmap but that's in microsecond difference. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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