0

I am trying to store the information of users when they create their accounts, until now I have an activity that when a users log in, checks if the user already exist on the database or not , and in case it doesn't exist it stores the new data on the database.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Obtain the FirebaseAnalytics instance.
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

        //Obtain the FirebaseAuth instance.
        mAuth = FirebaseAuth.getInstance();
        authUI = AuthUI.getInstance();

        FactoryMyVoto factoryMyVoto=new FactoryMyVotoFirebase();

        //Get user service 
        final UsuarioService uS= factoryMyVoto.getUsuarioService();


        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in

                       //PROBLEM HERE Check if the user is stored already on the database
                       Usuario userFirebase= uS.getUsuario(user.getUid());
                    if (userFirebase==null ){
                       uS.createNewUser();
                       Log.w("Login","usuario CREADO");

                    }else{
                        Log.w("Login","usuario RECONOCIDO");
                    }

                    Log.d("Login", "onAuthStateChanged:signed_in:" + user.getUid());
                    a.show();
                    startActivity(new Intent(LoginActivity.this, ListaPopuestasActivity.class));
                } else {
                    // User is signed out
                    b.show();
                    Log.d("Login", "onAuthStateChanged:signed_out");
                }
            }
        };

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

And the code on the user service is the following:

public Usuario getUsuario(String idUsuario) {
        final ArrayList<Usuario> usuarios=new ArrayList<>();

        DatabaseReference myRef = database.getReference(USUARIOS_FIREBASE).child(idUsuario);
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Usuario usuario = dataSnapshot.getValue(Usuario.class);
                usuarios.add(0,usuario);
                Log.d("UsuarioService","Usuario obtenido");
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d("UsuarioService","Usuario NO obtenido");
            }
        });
        if (!usuarios.isEmpty()){
            return usuarios.get(0);
        }else{
            return null;
        }
    }

The problem here is that when i call to the service , to check if the user is stored or not, I don't receive the data, the data is loaded after the method is call for first time , so all the times the activity is created I receive always that the user doesn't exist.

What do I have to do to receive the data at time? Just when I call the method.

And there is another way to return the data without using arrayList?

Regards and thanks in advance.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • The array `usuarios` will always be empty because `ValueEventListener.onDataChange` runs asynchronously, thus `usuarios.isEmpty` will runs before the listener has catched any value. – JP Ventura Dec 28 '16 at 10:23
  • thanks , but then how do i should solve this ? I mean how can i create kind of alert to know when is ready ? I am new to asynchronous programmation – Roberto Fernandez Diaz Dec 28 '16 at 10:28
  • http://stackoverflow.com/questions/37215071/firebase-android-how-to-read-from-different-references-sequentially/40557237 – JP Ventura Dec 28 '16 at 10:39

0 Answers0