bj4947
bj4947

Reputation: 920

Add data to specific location in Firebase Database

I have following database structure in Firebase. Its a pretty standard way to manage users and their data in database. I am also take advantage of FirebaseRecyclerAdapter in FirebaseUI.

-root
 -users
   -user1
     -item0
     -item1
     -item2
     ...
   -user2
   ...

Now I want to implement swipe to delete / undo feature. For the swipe to delete part, I had implemented ItemTouchHelper.SimpleCallback method to catch onSwiped status. In the callback I simply update the database using

mFirebaseAdapter.getRef(indexOfDeletedItem).removeValue();

In this way, the swiped item immediately got removed in the database. However, when I try to restore it back. I am not able to insert to where it was. For example,

@Override
        public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
            //get the location of swiped item. 
            final int indexOfDeletedItem = viewHolder.getAdapterPosition();

            //I cached the item in case I need to restore it.
            final ITEM deletedItem = mFirebaseAdapter.getItem(indexOfDeletedItem);

            //remove the item on swipe
            mFirebaseAdapter.getRef(indexOfDeletedItem).removeValue();

            Snackbar.make(findViewById(R.id.myCoordinatorLayout), "Deleted Item", Snackbar.LENGTH_LONG)
                    .setAction("UNDO", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                          //When user want to restored the deleted item
                          //I need to add the deleted data back to database.
                 mFirebaseAdapter.getRef(indexOfDeletedItem).push()setValue(deletedItem);

                                                   }
                         }).show();
    }

I am not able to correctly insert(add) the data into specific location without overwriting it. I think the index of item got updated immediately after I called mFirebaseAdapter.getRef(indexOfDeletedItem).removeValue();

For example, for user1 I have item0 with index 0,

item1 with index 1 and

item2 with index 2

 index     0      1         2

       item0 --> item1 --> item2   //now swipe on item1

       item0 --> item2             //now user click undo

       item0 --> item1             //item2 gets overwritten 

if there a way to make it back to

       item0 --> item1 --> item2 

In my case, I always overwrite the data on indexOfDeletedItem.

I am looking for a API to insert the node back to database without overwriting it. In other words, How can I push the node (item1) into where it was before?

Upvotes: 0

Views: 711

Answers (1)

Hhorn
Hhorn

Reputation: 223

The Problem is, that when you are restoring the item, the reference of the adapter is changed and you are receiving a different Reference. Just make a local copy of the Databasereference before you delete the item. E.g.

DatabaseReference ref = mFirebaseAdapter
getRef(indexOfDeletedItem);

Later inside the snackbar action you can just call:

ref.setValue(deletedItem);

Upvotes: 1

Related Questions