I have this structure of Firebase, and I am trying to update the Author in all post-comments when the user updates his nick_name.
I used this query in Android but it didn't fetch all the data that I need:
mDatabase = FirebaseDatabase.getInstance().getReference("DataBase").child("post-comments");
Query query = mDatabase.orderByChild("uid");
and down here you will find a picture of the Firebase structure:
As I understand from your comments, you trying to change the named of this node -KykDRTQ5GToIe6oh_kA to commentID. The short answer is no, there is no way in which you can change the names of the nodes from your Firebase database. There is no API for doing that. What can you do instead is to attach a listener on that particular node and get the DataSnapshot object. Having that data, you can write it in another place using another name. In the end just remove the old node. So you cannot simply rename -KykDRTQ5GToIe6oh_kA to commentID.
Related
Trying to fetch related data from firebase
I am new in Android and I am stuck here, want to retrieve data from Firebase where "del_id = 1" and "date = 2019-3-18" but I don't know how to write as Firebase way to retrieve what I want, Image above will further demonstrate what I have tried and what I want.
Your answer will be highly valuable for me.
The following line of code:
invSnapsjot.getValue(Invoice.class)
Returns an object of type Invoice and not a DataSnapshot object so you can call .child() method on it.
If you want to get a specific element (del_id = 1), you should simply use a query that looks like this:
myDatabase.orderByChild("del_id").equalTo("1").addValueEventListener(/* ... */);
Unfortunately, Firebase realtime database does not support queries on multiple properties but there is still a workaround for that, so please see my answer from the following post:
How to sort Firebase records by two fields (Android)
If you want to write or update value at that time you need to take .addValueEventListener
If you want to only set your value in firebase you directly set value without using .addValueEventListener
More details refer this link
https://firebase.google.com/docs/database/android/read-and-write
I have a database defined as follow
I can retrieve the cafeList as follow:
ApiManager.getInstance().mainDB.child(CafeModel.DATASET_NAME)
.orderByChild("name").startAt(searchText)
.endAt(searchText + "\uf8ff")
.addValueEventListener(new ValueEventListener()
However I am not sure how can I attach the fact that particular cafe is also set as favorite by my user.
Unfortunately, there is no way to achieve this in Firebase using only a single query. So in your case, you should query your database twice, once to get the cafe list and second to check if one of those cafe objects is favorite or not. However, there is a workaround in which you can create a new section in your user object named favoriteCafe in which you can add all user favorite cafes but this implies duplicating data. This practice is called denormalization and is a common practice when it comes to Firebase. For a better understanding, i recomand you see this video, Denormalization is normal with the Firebase Database. So in this case, if you want to know user favorite cafes, you can use a single query and attach a listener on this new created location.
Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
I was thinking about fetching data from Firebase and storing it on my phone using room library. But, I have a doubt in my mind. I want to use Firebase ChildEventListener because I want to fetch data only when a child is changed or added. But, I also know that ChildEventListener fetches all data when the app is started for the first time. So,my question is that in what cases firebase ChildEventListener will fetch whole data and in what cases a single child?. Actually, knowing this is important because if ChildEventListener fetches same data twice, room will give unique id duplication error.
If you cannot control when ChildEventListener is firing, an easy strategy would be to overwrite in all cases. You can avoid unique id duplication error with following code:
#Insert(onConflict = OnConflictStrategy.REPLACE)
If replace is not suitable for you there are other options.
Classes implementing ChildEventListener interface can be used to receive events about changes in the child locations of a given database reference. You should use this interface only if you want to respond to child-level changes because this interface has separate methods for when a child is added, removed, changed or moved (onChildAdded(), onChildRemoved(), onChildChanged() and onChildMoved() respectively).
In what cases firebase ChildEventListener will fetch whole data and in what cases a single child?
For example, onChildAdded() method is called once when you start the app to get all the children from a particular location but is also called everytime a new child is added at that location.
You can use below code to remove duplicate of data
#Entity(tableName = "post",indices = #Index(value = {"id"},unique = true))
In your model class you can give your "id" as unique so that same data will not store multiple times. Remember one point don't provide your primary key as unique you have to provide a separate id for your Room Database. hope it will help you :)
If I something like the following:
#Relation(parentColumn = "id", entityColumn = "fk_id", entity = User.class)
private List<User> users;
This list is a model which gets updated via LiveData.
But I want to be able to update this when I detect changes to the User table. Right now, it only picks up what exists in the DB, but whenever an update happens to the user table, this list does not get updated. Any suggestions?
You have not been very specific in what you are doing or want (which database you use, who is updating the database...).
But I want to be able to update this when I detect changes to the User
table. Right now, this only picks up what exists in the DB but
whenever an update happens to the user table, this list does not get
updated. Any suggestions?
Option 1
If you are doing the update/insert/delete in the same app as the List users, then you can easily update your List in your code.
Option 2
If another app/service is updating the database, I suggest you use a trigger to update your List.
See http://www.coderconsole.com/2015/02/android-sqlite-trigger-demo.html for an example.
Detecting the change in Java is tricky, see Calling a Java method from a sqlite trigger (android) .
Here is an example where you use a ContentObserver to monitor your Sqlite database (the flat file itself) https://gist.github.com/JBirdVegas/3874450
Option 3
If another app/service is updating the database, you can use a BroadcastReceiver to pick up the changes.
I have a Firebase Realtime Database with a set of Items who related to locations on a Googlemap. So every child in the Firebase have a data structure like this.
-item
latitude:"5511231165654"
longitude:"516891812013216"
postalcode: "10013"
cityname: "New York"
In my app every item from the database, where added to the related location on the Googlemap in form of a marker.
Now i think it is not a good idea to send all Items to all users. It´s more efficiently to send the user´s only these items that are relevant for them. These items are that one who are in the same area with the user, in this case same area means same postal code.
To handle this i build this Query
Query sortedByLocation = myRef.orderByChild("postalcode").equalTo(currentpostalcode);
sortedByLocation.addListenerForSingleValueEvent(
new ValueEventListener() {
.
. do something
.
}
i got the currentpostalcode from a Geocoder also the cityname. Now the problem is sometimes the Geocoder can´t decode the postal code and return null for it, but it allways return a cityname. So i changed the query to
Query sortedByLocation = myRef.orderByChild("cityname").equalTo(currentcity);
So how can i combine these two querys ? I want to do something like this:
is postalcode == null than check for cityname
The other Question will be, it is possible to retrieve data manually at need ?
Imagine the user start the app, the app now get all items who are in the same area with the user, but now the user left this area and go into a other area.
How can i trigger to retrieve the new data for this new area without to restart the app ?
Firebase Queries can only contain a single sort/filter condition.
Sometimes you can combine multiple values into a single property to get the behavior you want. For an example of that, see my answer here: Query based on multiple where clauses in firebase
But in general it sounds like you might be better off using GeoFire, which uses GeoHashes (a way of combining longitude and latitude into a single property) to allow filtering items based on their distance to a point. Note that at this moment, the updated version of GeoFire for Java is still in the works.