1

Here task (object) is being saved to fireBase Database, but for setting ListID and ID there r few conditions:

  1. if this is user's first task then set both to 20k

  2. if User already have task Lists in the firebase then add listId of thet task List to present task's listID and add (ID of user's last task + 1) to Id of present task.

What i want the sequence to be ( see Log.e(..) statements in the code ) :

1-> 2-> 3-> 4-> 5-> 6-> 7

What is actually happening : 1-> 4-> 5-> 7-> 2-> 3

(Also, Never going to 6 ... For which i reffered following : Can't reach some lines debugging android app & Android studio gradle breakpoint No executable code found at line ...... But all in vain)

( DBToFireb(..) is a method where changeListener is being used inside onComplete(..) of DatabaseReference.CompletionListener() )

public static boolean addTaskToFireBase(final Activity activity, final com.rb.eztask.model.TodoTask task, final ChangeListener changeListener)
    {

        mDatabase=FirebaseDatabase.getInstance().getReference("tasks");
        final String taskId = mDatabase.push().getKey();
        task.setTaskId(taskId);
        final DatabaseReference myFieBaseDBChecker=FirebaseDatabase.getInstance().getReference("users").
                child(Userkey).child("tasks");
        myFieBaseDBChecker.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String s=dataSnapshot.getValue().toString();
                if(s!=null)
                {
                    if(s.contentEquals(","))
                    {
                        task.setId(20000);
                        task.setListId(20000);
                        task.setKey(UserKey);
                        DBToFireb(activity, taskId, task, changeListener);
                    }

                    else
                    {   String tasks[]=s.split(",");
                        final String lastTask = tasks[tasks.length-1];
                        DatabaseReference compareDb;

                        for(int i=0;i<tasks.length;i++)
                        {
                            if(tasks[i].length()>2)
                            {Log.e("im","1");
                               compareDb=FirebaseDatabase.getInstance().getReference("task").child(tasks[i]);
                                compareDb.addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        Log.e("im","2");
                                        TodoTask taskToCompare=dataSnapshot.getValue(TodoTask.class);
                                        Log.e("im","3");
                                        if(taskToCompare.getBasketName().equals(task.getBasketName()))
                                        {
                                            task.setListId(taskToCompare.getListId());
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });
                            }
                        }


                        Log.e("im","4");
                        compareDb=FirebaseDatabase.getInstance().getReference("tasks").child(lastTask);
                        Log.e("im","5");
                        compareDb.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                Log.e("im","6");
                                TodoTask lastTaskInFireB = dataSnapshot.getValue(TodoTask.class);
                                    task.setListId(lastTaskInFireB.getListId()+1);

                                task.setId(lastTaskInFireB.getId()+1);

                                task.setKey(UserKey);
                                DBToFireb(activity, taskId, task, changeListener);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                        Log.e("im","7");
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return true;

    }
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • 1
    I don't know if you understand that Firebase is asynchronous – OneCricketeer Jun 30 '17 at 12:29
  • @cricket_007 how do i solve this issue? – Sukesh Panwar Jun 30 '17 at 12:42
  • Did you try printing out the value of `lastTask` before you did `getReference("tasks").child(lastTask);`? If you really want things executed in complete order, you'll have to write your code so that you only make a new Firebase listener *within* the previous one's onDataChange, not after it – OneCricketeer Jun 30 '17 at 12:45
  • Also, we can't tell that `String s=dataSnapshot.getValue().toString();` gets assigned to. Do you have a Task class? Why don't you do `Task t=dataSnapshot.getValue(Task.class);` if you do? – OneCricketeer Jun 30 '17 at 12:47
  • @cricket_007 lastTask is as expected it's just that sequence is the problem. Do i need to use AsynchTask class here? – Sukesh Panwar Jun 30 '17 at 12:50
  • No. Firebase doesn't need Asynctask, and it would only make things more complicated – OneCricketeer Jun 30 '17 at 12:51
  • Instead of explaining what your code does (or doesn't) , explain in words what you want it to do with a [mcve] – OneCricketeer Jun 30 '17 at 12:52

1 Answers1

0

onDataChange (and firebase callbacks in general) are invoked asynchonously

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
  • yeah, can u tell me what would be the solution to this? – Sukesh Panwar Jun 30 '17 at 12:40
  • Asynchronicity is a key concept in most applications....I'd recommend doing some initial reading up on that. You then need to make your application more reactive....i.e. design your logic to react to async updates/events. – John O'Reilly Jun 30 '17 at 12:42
  • There is no "solution" for asynchronous/reactive programming, the only way forward is to learn how it works. I recommend reading my answer here: https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Jun 30 '17 at 14:27