Just Get Newly added Child Firebase android - android

i am trying to get reference or snapshot of newly added child on firebase
i have solve it by different way
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
}
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s)
{
}
My question is that both are return a complete snapshot means all this nodes in firebase is there any way getting child which newly added ("this will improve speed")

When you attach a listener to a location or a query, the listener will initially fire with all data that exists at that location or that matches the query. The database has no concept of "new" data, although your application may have that concept.
There are a few options to reduce the data that is retrieved, or the data that is passed to your listener:
If you're using disk persistence, the application may have a previous snapshot of the data in its disk cache. In that case, only the delta between the previous snapshot and the current snapshot is retrieved from the server. But the entire current snapshot is passed to your listener, so that you can render the UI correctly for the while current data.
If you only want to process the data since a certain moment, you'll need to only request that data from the database. You can typically do this by storing the key of the data you last processed in your local state, and then using a query like this to only get the new update:
myRef.orderByKey().startAt("mostRecentKeyWeHaveAlreadyProcessed").add...Listener

Related

Populating Room Database from Google Firebase

I'm using both Google Firebase and Room Database to store Event objects I have created, containing all the details of a given event. I use Firebase for online storage and user interaction, and I use Room Database for an easier, persistent RecyclerView implementation.
My problem is that my function for populating the Room Database with events in the user's radius doesn't seem to execute at all in NEITHER an asynchronous task NOR a Room Database callback that overrides the onCreate method. The function was fully debugged while it was used on the main thread, so I think the reason why it isn't working right now has to do with my lack of understanding of how asynchronous tasks work.
Here is my function, currently within the onCreate method of the Room Database class:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference().child("events").child("eventid");
if (ref != null) ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Event event = dataSnapshot.getValue(Event.class);
if (event != null) {
LatLng user_location = new LatLng(current_user.get(0).getLatitude(),
current_user.get(0).getLongitude());
LatLng event_location = new LatLng(event.getLatitude(), event.getLongitude());
int distance_preference = current_user.get(0).getDistance();
double distance_between_user_and_event = SphericalUtil
.computeDistanceBetween(user_location, event_location) / 1609.344;
if (distance_between_user_and_event <= distance_preference) {
eventDao.insert(event);
}
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
In an asynchronous task, I can't really debug it since it isn't on the main thread (unless I don't know how lol), and this code doesn't seem to execute at all when in the onCreate method for Room Database. There's obviously something I'm missing here.
Also, it's worth saying that my EventDao, EventDatabase, and EventRepository have all been fully debugged. I can use all of them perfectly fine at runtime -- it's just this early population task that isn't working!
Thanks so much for any help!!
Similar project created by me which uses
Firebase authentication to login user
Save and cache user notes to
sql lite database with Room
Save user notes to firebase base
database
✍️ Simple Note Making App use Sqllite Room 🧰 for caching the notes and 📥 Firebase Database for online storage
https://github.com/LanguageXX/Simple-Note-App-with-Online-Storage
Hope that helps you

How to get only newly added data from the Firebase?

As i am creating the chat app, for it i am using the Firebase.
It works perfectly, only one problem which i am getting that each time i am getting whole list of data from Firebase, when new item is added or deleted into it.
What i want that , only newly added record should come from Firebase , not whole list of data.
Please check my below code for it
ArrayList<ChatMessage> MY_ARRAYLIST= new ArrayList<>();
MsgViewHolder viewModel = ViewModelProviders.of(this).get(MsgViewHolder.class);
LiveData<DataSnapshot> liveData = viewModel.getDataSnapshotLiveData();
liveData.observe(this, dataSnapshot -> {
if (dataSnapshot != null) {
MY_ARRAYLIST.clear(); // I NEED TO CLEAR THE ARRAY-LIST TO GET THE REFRESHED DATA..I DO NOT WANT TO LOAD WHOLE LIST ON EACH TIME
if (dataSnapshot.exists())
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ChatMessage bean = ds.getValue(ChatMessage.class);
assert bean != null;
MY_ARRAYLIST.add(bean);
}
}
});
As in above code i am using MY_ARRAYLIST.clear() to clear data, to get the newly added record.On each time from DataSnapshot , i am getting the whole list of data, whenever new record added or deleted.Is their any method to get only newly added data NOT whole List from Firebase?
You can use addChildEventListener() to you DatabaseReference which will notify you on different callback method when any child is add/removed/updated/deleted or moved
databaseRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
// If any child is added to the database reference
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// If any child is updated/changed to the database reference
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// If any child is removed to the database reference
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
// If any child is moved to the database reference
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("FirebaseListAdapter", "Listen was cancelled, no more updates will occur");
}
});
The DataSnapshot in each callback will provide you the information of the child
This is by design, in a real-time system there is no concept of the "latest" data because it's always changing. However, if you want to only display items added to the list after the page has loaded, you can do the following:
var newItems = false;
var eventsList = new Firebase('https://*****-messages.firebaseio.com/');
eventsList.on('child_added', function(message) {
if (!newItems) return;
var message = message.val();
$.notification(message.message);
});
eventsList.once('value', function(messages) {
newItems = true;
});
above answer original by (anant)
(my) another solution
if you have control over your database schema you can add a 'datetime' element in your object and store the value of the time just before adding it to database in Epoch format, then you can simply get the list of objects in newly added order with limit like this.
ds.orderBy("datetimeSent", Direction.DESCENDING).limit(10);
I think you can use this listener onChildAdded() I provided a link to the documentation.
Listen for child events
You can try Query for query on database,add on child as date and time and query based on that data filed after every result update the query to last sync date and time

Firebase: Read Data once activity started

is there any ways to read data from Firebase once the Activity is loaded. At this moment I am using the regular valueEventListener, but in order for it to work, there has to be some sort of a change in the database
mDatabaseReference.child("Users").child(mUser.getUid()).
child("Posts").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayOfQuestionForms.clear();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
QuestionForm tempQuestionForm = postSnapshot.getValue(QuestionForm.class);
arrayOfQuestionForms.add(tempQuestionForm);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
What I am looking for is some way to read data from Firebase without using listeners. I have looked at other similar posts but I don't think there is a clear answer for this yet.
There is no way for reading data from a Firebase database without using listeners. Everything is about listeners when it comes to Firebase. It's true that when setting a value, we just need to use a method named setValue() directly on the reference. Unfortunately, there is no method within Firebase, let' say getValue(), which acts in the same way as setValue().
To solve this, i recommend you using addListenerForSingleValueEvent.
Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.
in order for it to work, there has to be some sort of a change in the database
This is not true and a common source of confusion for developers.
With your current code, Firebase will immediately start reading the data from the server. Once it gets that data, it invokes your onDataChange().
From the documentation:
This method is triggered once when the listener is attached and again every time the data, including children, changes.
for such purpose I used different kind of listener - ChildEventListener. It has different #Override methods. The method onChildAdded returns every child-nodes of the node when called first time (i.e. on activity start).
Put attention - maybe you will need to slightly change the reference to DB (trim back one hierarchy level), to point to the parent node. If you expanded snapshot of your DB structure, I can look.
Here is updated code (sorry is made any typo - I couldn't test it as have no your DB :)
mDatabaseReference.child("Users").child(mUser.getUid()).child("Posts").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
arrayOfQuestionForms.clear();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
QuestionForm tempQuestionForm = postSnapshot.getValue(QuestionForm.class);
arrayOfQuestionForms.add(tempQuestionForm);
}
}
#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) {
}
});

firebase retrieve complex structure

Hello I am new to Firebase database. Currently I am developing a android application. I am looking for a help, Below is my Firabase Data Structure. Can anyonce please tell me how to retrive the data.
Requirement is I have to retrieve the all the companies. Upon selecting the a specific company, it has to fetch the corresponding company coordinates into polygon shape variable.
Please Help
Why not have a structure like:
companyName/info
Instead of
Firebase ID/Companyname/info
i.e use update or set instead of push
public class CompanyIds{
public List<Company> companies;
}
FirebaseDatabase.getInstance()
.getReference("parent node path of these ID's");
Now in FirebaseRecyclerAdapter what you can do is:-
#Override
protected void populateViewHolder(final MessagesHolder viewHolder, CompanyIds model, int position) {
Company = model.companies.get(0);
//Set data on your view from Company model
}
As your ID is having only one company then you get that only company from the first index.
Now after clicking on any item, you can easily get the details for a company like coordinates or anything else in the same way you set the data in the adapter. It won't be a problem.
public class Company{
List<String> Cordinates;
List<Employees> Employees;
//Define other fields
}
Basically I got the answer from repetitive hits and tries while working on the similar problem.
Look when you go for general approach of add addValueEventListner(), you get a snapshot of the database with respect to your root node. Thus on the first addValueListner() callback, get the reference of your child node and then addChildEventListner() on it. So here is what i suggest:
DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListner(new ValueEventListner(){
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot){
//here your snapshot is considered with respect to root. So you get the reference of child node now
datasnapshot.child("Bureau 507").getRef().addChildEventListner(new
ChildEventListner){
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s){
for(DataSnapshot data: dataSnapshot.getChildren()){
//Now you are at the subtree of Bureua 507. So now you get your data in the form of
key and values
}
//remaining callbacks

How to retrieve value from the highest/last level node using 'ChildEventListener' from Firebase?

I have several strings stored under specific reference: mReference.child(rID).child(userID2) which I want to retrieve using childEventListener as I am doing some task also when these string gets removed and that is only possible with onChildRemoved of ChildEventListener.
Here's what I have tried:
mReference.child(rID).child(userID2).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("dataHEre", dataSnapshot.getValue().toString());
}
}
The problem is that I'm unable to retrieve data using keys here as dataHEre is logging out:
D/dataHEre: 28.8419556
D/dataHEre: 78.779063
D/dataHEre: 3
D/dataHEre: Railway Colony
D/dataHEre: Sleep
D/dataHEre: 12:36 AM
D/dataHEre: Superman
which are values?
So, I want to know how can I retrieve data here using keys and using ChildEventListener and then assign the data retrieved to various strings?
I think that you are doing your query in the wrong way. onChildAdded method is retrieving you each child of a specific value (userID2 I suppose). If that is what you want, then just use a onValueEventListener() to retrieve the whole dataSnapshot of your userId2 node each time that it changes.
If you want to retrieve the data just once, you should use onSingleValueEventListener(). And OnChildEvent() is used to retrieve lists of data where you want to track each of the child individually. For example if you attach an OnChildEventListener to mReference.child(rID) you will get a OnChildAdded for each userId, what is pretty usefull like for example fill a RecyclerView or a ListView being able to update each item individually together with your Firebase Data.
If i'm not wrong what you want is just get updates of your userId2 reference, in that case attach a OnValueEventListener to that reference and you will get a call each time a value is modified, deleted, or added.
firebaseDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override public void onDataChange(DataSnapshot dataSnapshot) {
customPOJO customPOJO = dataSnapshot.getValue(YourCustomPOJO.class);
customPOJO.getName();
customPOJO.getEmail();
customPOJO.getfavoriteFood();
//and so on....
}
#Override public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources