I have this item in the database
And I have a method for removing items, but is not working, this is the method
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
It gets the reference well, but the method setValue(null) is doing nothing (I've also tried the removeValues().
I don't get any exception or some kind of response, I hope you can help me.
Thanks!
I beleive you have a model class that inserts your items into the database.
so what you have to do is to use that class to reference the object directly and perform the required action on it. Something like this.
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This shouldn't be there
//dataSnapshot.getRef().setValue(null);
//instead use the modelclass you used inserting data, to reference the object
Modelclass mode = dataSnapshot.getValue(Modelclass.class);
/** Note this line indicates the getter and setter method for the particular object that is being referenced**/
mode.setItem(null)
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
When you execute a query against the Firebase Database, there will potentially be multiple results. You're using a ValueEventListener, so the snapshot contains a list of all results. Even if there is only a single result, the snapshot will contain a list of one result.
You need to loop over DataSnapshot.getChildren() to get the individual items matching your query:
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
child.getRef().removeValue();
}
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
Related
I am creating an android app using firebase. My firebase database structure is as follow-
Users:{
uid:{
name:Bruno,
age: 22,
email: bruno#gmail.com,
address:...
}
},
Level_info:{
uid:{
score:70,
level:5
}
}
Before Rxjava, I retrieve all users like this-
userRef.addValueEventListener(new ValueEventListerner(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(Datasnapshot snapshot:dataSnapshot.getChildren())
{
User user = snapshot.getValue(User.class);
userList.add(user);
//to get score and levels
/*I can't add nested value event listener directly because of Asynchronous behavior */
userId.add(user.getId());
}
getLevels(userId);
}
#Override
public void onCancelled(DatabaseError error) {
//handle errors
}
});
void getLevels(final ArrayList<String> userid){
levelRef.addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(String id:userid)
{
Level level = dataSnapshot.child(id).getValue(Level.class);
levelList.add(level);
}
/** Attch both "userList" and "levelList" to RecyclerView */
}
#Override
public void onCancelled(DatabaseError error) {
//handle errors
}
});
}
It goes fine.But if there are many nested Listeners, It will hard to read.So I tried to change to RxJava and RxAndroid. Like this-
public Observable<ArrayList<User>> userOb(){
return Observable.create(new
ObservableOnSubscribe<ArrayList<User>>() {
#Override
public void subscribe(final
ObservableEmitter<ArrayList<User>> emitter)
throws Exception {
FirebaseDatabase.getInstance().getReference("Users")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ArrayList<UserModel> listUsers;
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
listUsers= new ArrayList<>();
listUsers.add(user);
emitter.onNext(listUsers);
}
emitter.onComplete();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
Here I don't know how to zip with LevelObservable because levelObservable need UserId from UserObservable to get data from Levels/(uid)/"level_info" .May be this is stupid question but I am new to RxJava. I want to get Corresponding userId from UserObservable to LevelObservable. So how can I do that?
Sorry for my bad English Writing skill.
Edit: Is there some ways to implement like this (because may be I miss some easy ways to implement).
I'm facin a really weird problem, Im calling two nodes in my database , from one node I get all my user ids , then I just loop inside my user node to get the user metadata. The problem is that when I run the debugger, it seems like the mAdapter is beign called before the data is beign fetched. So my adapter set the recyclerview with no data. After the adapter has been called, my query fetching the user begins and the array is populated normally
This is what I have done so far
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
if(mAdapter.getItemCount()>0)
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
As you can see, I set the adapter to the recyclerview after the first foreach that fetchs all the data, I know that beign asynchronous I need to wait for the inner mDatabase to be done but if I move the setting of the adapter one line above to wait for the second call to finish it does the same, any idea?
Database structure is like this
teamsNode
|__Jm2KSMjslpow2Ipoasz : true
|__601KSMjsldfjd2Ipos0 : true
|__asgm2Kshalpow2IposJ : true
users
|__Jm2KSMjslpow2Ipoasz
|_____name: randomname1
|__601KSMjsldfjd2Ipos0
|_____name: randomname2
|__asgm2Kshalpow2IposJ
|_____name: randomname3
Recyclerview is not populated. Any idea?
A weird thing is that sometimes runing it with the debugger does fill the data, but its rarely to happend.
If you want to only initialize the adapter when all data is loaded, you will need to do so in the nested onDataChange callback:
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshotTeam) {
AtomicInteger loadedUserCount = new AtomicInteger(0);
for(DataSnapshot snapshot : dataSnapshotTeam.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
if (loadedUserCount.getAndIncrement() + 1 == dataSnapshotTeam.getChildrenCount()) {
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
if(mAdapter.getItemCount()>0) {
mRecyclerView.setAdapter(mAdapter);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
The problem you have is that the calls to the addListenerForSingleValueEvent methods are asynchronous, so they are executed in another thread. When you call the second addListenerForSingleValueEvent this is executed in another thread, while the main thread follows its execution. You have to do the call of your adapter in the answer of your second call addListenerForSingleValueEvent in the onDataChange method. In this way to avoid anomalous operations of your application.
Hope this will help.
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
mArrayPalyer.clear();//add this
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
mRecyclerView.setAdapter(mAdapter); //makes changes here because you are checking for adapter item count before setting it and this always return null.
}
You can look at my code I am using ValueEventListener just like you and binding the data to recycler view
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
UserListModel userListModel =dataSnapshot1.getValue(UserListModel.class);
userModelArrayList.add(userListModel);
}
userListAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(">>value", "loadPost:onCancelled", databaseError.toException());
}
};
//mDatabase is the reference to the Firebase Node
mDatabase.addValueEventListener(postListener);
userListRecyclerView.setLayoutManager(M.gridLayoutRecyclerView(getActivity() , 2));
userListAdapter = new UserListAdapter(getActivity(), userModelArrayList);
userListRecyclerView.setAdapter(userListAdapter);
I'm trying to retrieve the child below "ticket" with the key in between but not able to do it. Method getKey() is return "ticket" instead of the key.
private void getSpecificTicketFromFirebase() {
Timber.d("Inside Pull Data %s ",firebaseManager.getFireBaseUser().getUid()) ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("user").child(firebaseManager.getFireBaseUser().getUid()).child("ticket");
FirebaseDatabase.getInstance().getReference().addValueEventListener(new ValueEventListener() {
int i=0;
#Override public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
passengerData = data.getValue(PassengerViewModel.class);
passengerViewModel.add(passengerData);
Timber.d("Passenger Name %s index %s key %s",passengerViewModel.get(i).getFromStationName(),i,data.child("passengerName"));
i++;
}
}
#Override public void onCancelled(DatabaseError databaseError) {
Timber.e(databaseError.getDetails());
Timber.e(databaseError.getMessage());
Timber.e(databaseError.toException());
}
});
}
If you only want to get those objects of PassengerViewModel class, there is no need to use the child() method. Just remove this call: .child(ref.getKey()) and your code will work perfectly fine.
If you want to pass that pushed key to the child() method, you need to store it first into a variable. To achieve this, please use the following code:
String key = ref.push().getKey();
Once you have this key, you can use it in any reference.
First of all Thank you #AlexMamo....
Here is the correct way to do it.....
private void getSpecificTicketFromFirebase() {
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference().child("user").child(firebaseManager.getFireBaseUser().getUid()).child("ticket");
ref.addValueEventListener(new ValueEventListener() {
#Override public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Timber.d("Key %s",data.getKey());
}
}
#Override public void onCancelled(DatabaseError databaseError) {
Timber.e(databaseError.toException());
}
});
I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...
reference = FirebaseDatabase.getInstance().getReference("Users");
Query query = reference.orderByKey().equalTo(task.author_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Structure
You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I'm looking to search through my Firebase database and find a field:value pairing that matches my query, and then return either that parent's key, or the parent object so that I may grab other information as well.
The Firebase database looks something like this:
Events{
-KiXlIGhB6k-HpCKfO3n{
name:"Breakfast at Tiffany's",
owner:"Tim",
startTime:{
startHour:1,
startMinute:30
},
...
},
-dFgfh8Efa-Hpwe6Goqp0{...}
}
I'm currently attempting:
public void importSchedule(String ownerName){
DatabaseReference events =
FirebaseDatabase.getInstance().getReference("Events"); //Inside the Events list
Query allOwnersEvents = events.equalTo(ownerName); //Find events equalTo ownerName provided
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
// This is where the parent's could be queried, all events belonging to an "owner" should be cycled through
}
}
public void onCancelled(DatabaseError databaseError) {}
});
However when placing a break-point inside the for loop, it is never triggered. I'm wondering if I'm attempting the query correctly or if there's an easier way to accomplish this.
It's never triggered because your DatabaseReference is wrong. When you query, you are missing a child. In order to have the correct DatabaseReference please use this code:
DatabaseReference events = FirebaseDatabase.getInstance().getReference("Events")
events.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String eventKey = ds.getKey(); //parent key
DatabaseReference allOwnersEvents = FirebaseDatabase.getInstance().getReference("Events").child(eventKey);
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String ownerName = dataSnapshot.child("ownerName").getValue(String.class); //do what you want with ownerName
}
public void onCancelled(DatabaseError databaseError) {}
});
}
}
public void onCancelled(DatabaseError databaseError) {}
});
In which eventId is the unique id generated by the push() method. Hope it helps.