Jacin Montava
Jacin Montava

Reputation: 109

Firebase helper class

I am practicing with the Firebase library and I would like to be able to have some methods in a separated class from the Main and from this to be able to call those methods and to return for example the user.

Example FirebaseDBHelper.java

public static UserModel getUserFromId(String userId) {
    final UserModel[] user = new UserModel[1];
    dbRef.child("status").child(userId).addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // Get user value
                    user[0] = dataSnapshot.getValue(UserModel.class);
                    Log.d("TAG", "Value is: " + dataSnapshot.getValue());
                    Log.d("TAG", "User: " + user);
                    // ...
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.d("TAG", "getUser:onCancelled", databaseError.toException());
                    // ...
                }
            });
    return user[0];
}

Main call

UserModel user = FirebaseDatabaseHelper.getUserFromId("9876");
Log.d("TAG", "UserInfo: " + user);

But I have some problems because the method getUserFromId() don't return anything.

Upvotes: 1

Views: 2523

Answers (2)

Albert
Albert

Reputation: 343

Better way is to use callback from the helper class create and interface and let ure activity implement it when you call the static method pass the class when firebase gets the data call the activity callback...

Upvotes: 1

phatnhse
phatnhse

Reputation: 3920

If you know about RxJava2, it really good to go with this approach:

public Observable<UserModel> UserModel getUserFromId(String userId) {
    return Observable.create(new ObservableOnSubscribe<UserModel>() {
        @Override
        public void subscribe(@NonNull ObservableEmitter<UserModel> e) throws Exception {
            dbRef.child("status").child(userId).addListenerForSingleValueEvent(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // Get user value
                            UserModel user = dataSnapshot.getValue(UserModel.class);
                            e.onNext(user);
                            // ...
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Log.d("TAG", "getUser:onCancelled", databaseError.toException());
                            e.onError(databaseError);
                            // ...
                        }
                    });
        }
    });
}

OR, you can define an interface callback when you get your UserModel like this

public static void getUserFromId(String userId,OnGetUser callback) {
    dbRef.child("status").child(userId).addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // Get user value
                    user[0] = dataSnapshot.getValue(UserModel.class);
                    Log.d("TAG", "Value is: " + dataSnapshot.getValue());
                    Log.d("TAG", "User: " + user);
                    callback.onGetUser();
                    // ...
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.d("TAG", "getUser:onCancelled", databaseError.toException());
                    // ...
                }
            });
}

interface OnGetUser
{
    void onGetUser();
}

OR, user[0] should have some value when onCancelled was called, like empty UserModel

Upvotes: 6

Related Questions