Firebase realtime database addChildEventListener is not working












0















I am using a service for my work and my aim is i need to load all details from server and when new thing is add ,changed or removed
so i use below code inside service



Service code



public class ChildEventListenerNew extends Service {
public ChildEventListenerNew() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

Handler handler = new Handler();
int delay = 1000; //milliseconds

DatabaseReference firebaseDatabase;
int ct = 0, ct2 = 0;
boolean internet = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("product_table_milla", "onStartCommand " + " is called ct:" + ct);
firebaseDatabase = FirebaseDatabase.getInstance().getReference("product_table");

handler.postDelayed(new Runnable() {
public void run() {
//do something
ct2++;
if (isOnline() && internet == false) {
internet = true;
Log.i("product_table_milla", "handler " + " isonline and internet is false ct:" + ct2);

setLinsier();

} else if (isOnline() == false) {
internet = false;
Log.i("product_table_milla", "handler " + " isoffline and internet is false ct:" + ct2);


}
Log.i("product_table_milla", "handler " + " is called ct:" + ct2);
handler.postDelayed(this, delay);
}
}, delay);


//return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

ChildEventListener childEventListener = null;

private void setLinsier() {
if (childEventListener != null) {
firebaseDatabase.removeEventListener(childEventListener);
}
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildAdded " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildAdded is null");
}
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildChanged " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildChanged is null");
}
}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildRemoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildRemoved is null ");
}

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildMoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildMoved is null");
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
ct++;
Log.i("product_table_milla", "onCancelled " + databaseError.getMessage() + "" + " ct:" + ct);

}
};
firebaseDatabase.addChildEventListener(childEventListener);

}

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
return false;
}
return true;
}}


Manifest file i added this code



<service android:name=".services.ChildEventListenerNew">
</service>




And in my main activity



if(isMyServiceRunning(ChildEventListenerNew.class)){

}else {
startService(new Intent(getApplicationContext(),ChildEventListenerNew.class));
}


Every thing was working fine , but when i disable internet connection and re enable internet connection addChildEventListener is not working. but the code "postDelayed" is working which means after closing my app and internet connection service is working fine, but firebaseDatabase.addChildEventListener is not responding. i try by adding ,changing and deleting details in databse but nothing happened.
What is wrong with my code?
Please help?
I need to get all new added data and changed or deleted data details when my using connect to internet . Is that possible with firebase reailtime database code?










share|improve this question

























  • This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

    – Itapox
    Nov 27 '18 at 18:20
















0















I am using a service for my work and my aim is i need to load all details from server and when new thing is add ,changed or removed
so i use below code inside service



Service code



public class ChildEventListenerNew extends Service {
public ChildEventListenerNew() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

Handler handler = new Handler();
int delay = 1000; //milliseconds

DatabaseReference firebaseDatabase;
int ct = 0, ct2 = 0;
boolean internet = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("product_table_milla", "onStartCommand " + " is called ct:" + ct);
firebaseDatabase = FirebaseDatabase.getInstance().getReference("product_table");

handler.postDelayed(new Runnable() {
public void run() {
//do something
ct2++;
if (isOnline() && internet == false) {
internet = true;
Log.i("product_table_milla", "handler " + " isonline and internet is false ct:" + ct2);

setLinsier();

} else if (isOnline() == false) {
internet = false;
Log.i("product_table_milla", "handler " + " isoffline and internet is false ct:" + ct2);


}
Log.i("product_table_milla", "handler " + " is called ct:" + ct2);
handler.postDelayed(this, delay);
}
}, delay);


//return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

ChildEventListener childEventListener = null;

private void setLinsier() {
if (childEventListener != null) {
firebaseDatabase.removeEventListener(childEventListener);
}
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildAdded " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildAdded is null");
}
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildChanged " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildChanged is null");
}
}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildRemoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildRemoved is null ");
}

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildMoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildMoved is null");
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
ct++;
Log.i("product_table_milla", "onCancelled " + databaseError.getMessage() + "" + " ct:" + ct);

}
};
firebaseDatabase.addChildEventListener(childEventListener);

}

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
return false;
}
return true;
}}


Manifest file i added this code



<service android:name=".services.ChildEventListenerNew">
</service>




And in my main activity



if(isMyServiceRunning(ChildEventListenerNew.class)){

}else {
startService(new Intent(getApplicationContext(),ChildEventListenerNew.class));
}


Every thing was working fine , but when i disable internet connection and re enable internet connection addChildEventListener is not working. but the code "postDelayed" is working which means after closing my app and internet connection service is working fine, but firebaseDatabase.addChildEventListener is not responding. i try by adding ,changing and deleting details in databse but nothing happened.
What is wrong with my code?
Please help?
I need to get all new added data and changed or deleted data details when my using connect to internet . Is that possible with firebase reailtime database code?










share|improve this question

























  • This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

    – Itapox
    Nov 27 '18 at 18:20














0












0








0








I am using a service for my work and my aim is i need to load all details from server and when new thing is add ,changed or removed
so i use below code inside service



Service code



public class ChildEventListenerNew extends Service {
public ChildEventListenerNew() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

Handler handler = new Handler();
int delay = 1000; //milliseconds

DatabaseReference firebaseDatabase;
int ct = 0, ct2 = 0;
boolean internet = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("product_table_milla", "onStartCommand " + " is called ct:" + ct);
firebaseDatabase = FirebaseDatabase.getInstance().getReference("product_table");

handler.postDelayed(new Runnable() {
public void run() {
//do something
ct2++;
if (isOnline() && internet == false) {
internet = true;
Log.i("product_table_milla", "handler " + " isonline and internet is false ct:" + ct2);

setLinsier();

} else if (isOnline() == false) {
internet = false;
Log.i("product_table_milla", "handler " + " isoffline and internet is false ct:" + ct2);


}
Log.i("product_table_milla", "handler " + " is called ct:" + ct2);
handler.postDelayed(this, delay);
}
}, delay);


//return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

ChildEventListener childEventListener = null;

private void setLinsier() {
if (childEventListener != null) {
firebaseDatabase.removeEventListener(childEventListener);
}
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildAdded " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildAdded is null");
}
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildChanged " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildChanged is null");
}
}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildRemoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildRemoved is null ");
}

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildMoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildMoved is null");
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
ct++;
Log.i("product_table_milla", "onCancelled " + databaseError.getMessage() + "" + " ct:" + ct);

}
};
firebaseDatabase.addChildEventListener(childEventListener);

}

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
return false;
}
return true;
}}


Manifest file i added this code



<service android:name=".services.ChildEventListenerNew">
</service>




And in my main activity



if(isMyServiceRunning(ChildEventListenerNew.class)){

}else {
startService(new Intent(getApplicationContext(),ChildEventListenerNew.class));
}


Every thing was working fine , but when i disable internet connection and re enable internet connection addChildEventListener is not working. but the code "postDelayed" is working which means after closing my app and internet connection service is working fine, but firebaseDatabase.addChildEventListener is not responding. i try by adding ,changing and deleting details in databse but nothing happened.
What is wrong with my code?
Please help?
I need to get all new added data and changed or deleted data details when my using connect to internet . Is that possible with firebase reailtime database code?










share|improve this question
















I am using a service for my work and my aim is i need to load all details from server and when new thing is add ,changed or removed
so i use below code inside service



Service code



public class ChildEventListenerNew extends Service {
public ChildEventListenerNew() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

Handler handler = new Handler();
int delay = 1000; //milliseconds

DatabaseReference firebaseDatabase;
int ct = 0, ct2 = 0;
boolean internet = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("product_table_milla", "onStartCommand " + " is called ct:" + ct);
firebaseDatabase = FirebaseDatabase.getInstance().getReference("product_table");

handler.postDelayed(new Runnable() {
public void run() {
//do something
ct2++;
if (isOnline() && internet == false) {
internet = true;
Log.i("product_table_milla", "handler " + " isonline and internet is false ct:" + ct2);

setLinsier();

} else if (isOnline() == false) {
internet = false;
Log.i("product_table_milla", "handler " + " isoffline and internet is false ct:" + ct2);


}
Log.i("product_table_milla", "handler " + " is called ct:" + ct2);
handler.postDelayed(this, delay);
}
}, delay);


//return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

ChildEventListener childEventListener = null;

private void setLinsier() {
if (childEventListener != null) {
firebaseDatabase.removeEventListener(childEventListener);
}
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildAdded " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildAdded is null");
}
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildChanged " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildChanged is null");
}
}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildRemoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildRemoved is null ");
}

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ct++;
if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
Log.i("product_table_milla", "onChildMoved " + dataSnapshot.getKey() + " " + dataSnapshot.child("p_name").getValue(String.class) + " ct:" + ct);
} else {
Log.i("product_table_milla", "onChildMoved is null");
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
ct++;
Log.i("product_table_milla", "onCancelled " + databaseError.getMessage() + "" + " ct:" + ct);

}
};
firebaseDatabase.addChildEventListener(childEventListener);

}

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
return false;
}
return true;
}}


Manifest file i added this code



<service android:name=".services.ChildEventListenerNew">
</service>




And in my main activity



if(isMyServiceRunning(ChildEventListenerNew.class)){

}else {
startService(new Intent(getApplicationContext(),ChildEventListenerNew.class));
}


Every thing was working fine , but when i disable internet connection and re enable internet connection addChildEventListener is not working. but the code "postDelayed" is working which means after closing my app and internet connection service is working fine, but firebaseDatabase.addChildEventListener is not responding. i try by adding ,changing and deleting details in databse but nothing happened.
What is wrong with my code?
Please help?
I need to get all new added data and changed or deleted data details when my using connect to internet . Is that possible with firebase reailtime database code?







android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 14:51









Frank van Puffelen

239k29382408




239k29382408










asked Nov 27 '18 at 13:16









MidhilajMidhilaj

7661722




7661722













  • This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

    – Itapox
    Nov 27 '18 at 18:20



















  • This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

    – Itapox
    Nov 27 '18 at 18:20

















This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

– Itapox
Nov 27 '18 at 18:20





This does not answer your question, but is there any reason for you to restart the listener at every fall of the internet? What if you used offline with set persistent on in your database?

– Itapox
Nov 27 '18 at 18:20












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500607%2ffirebase-realtime-database-addchildeventlistener-is-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500607%2ffirebase-realtime-database-addchildeventlistener-is-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista