0

So I'm currently trying to make a Firebase database for an Android Studio app consisting of multiple UserProfile Class Objects, each containing 4 properties, that is username, password, secretQuestion and secretAnswer. I can add these objects to database with each having a unique key, having no problem with that but I need to check every one of them when signing in. I made a retrieving method with ValueEventListener that returns a UserProfile list. When I click sign-in in my login page the method always returns a null list and after restarting the app or playing with some pages, app crashes when I try to sign in. I can't find the problem with the method unfortunately...

Here is my method for getting the data:

 public static List<UserProfile> readUserProfile()
{
    final List<UserProfile> profiles = new ArrayList<UserProfile>();

    DatabaseReference getProfile;
    getProfile = CreateProfile1.database.getReference();
    getProfile.child( "UserProfiles").addValueEventListener( new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for ( DataSnapshot child : children )
            {
                UserProfile aProfile = child.getValue( UserProfile.class);
                profiles.add( aProfile);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {
            Log.e("The read failed: ", databaseError.getMessage());
        }
    });

    return profiles;
}

And here is my sign-in checking:

loginProfiles = CreateProfile2.readUserProfile();

                for ( int i = 0; i < loginProfiles.size(); i++ )
                {
                    if ( loginProfiles.get( i).getUsername().equals( usernameField.getText().toString() ) &&
                            loginProfiles.get( i).getPassword().equals( passwordField.getText().toString() ) )
                    {
                        Intent goToMainPage;
                        goToMainPage = new Intent(LoginPage.this, MainMenu.class);

                        startActivity(goToMainPage);
                    }
                }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Firebase database is asynchronous it will always return empty list – Salman500 May 03 '18 at 19:49
  • see this article : https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93 – Salman500 May 03 '18 at 20:03
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo May 04 '18 at 13:15

1 Answers1

0

Firebase database is asynchronous do something like this, Use FirebaseAuth if you want to sign in . Firebase Auth

     getProfile.child( "UserProfiles").addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s){

                        UserProfile userProfile = dataSnapshot.getValue( UserProfile.class);
                        if ( userProfile.getUsername().equals( usernameField.getText().toString() ) &&
                                userProfile.getPassword().equals( passwordField.getText().toString() ) )
                        {
                            Intent goToMainPage = new Intent(LoginPage.this, MainMenu.class);

                            startActivity(goToMainPage);
                        }

                        }

                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
Salman500
  • 1,213
  • 1
  • 17
  • 35
  • Unfortunately it still crashes. Does onChilAdded invoke when I call the method each time because the listener attaches to it, just like ValueEventListener? Also how can I check it for every single user profile in the database with this one? Doesn't it just check it for only 1 child? – Talhs Şen May 03 '18 at 20:42
  • you have to call the method once , when the new child is added onChildAdded will be called, if child is changed onChildChange will be call – Salman500 May 04 '18 at 07:19