Issue
My app is checking if a certain data is in the Firebase Realtime Database with a addListenerForSingleValueEvent added to a database reference. The emulator in which i started the project works fine and retrieves the data perfectly but when i change the emulator (let's say i switch to PIXEL 3XL) the listener doesn't work. I've seen in another stackoverflow question that someone had the same problem and the database retrieved data but after a long time. Does anyone know why this happens? Should i keep on developing with the default emulator and not care about the realtime database not working on another emulator? Can you explain why this happening?
I use this in the manifest:
...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>...
Gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle(Module)
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "..."
minSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:19.3.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Edit 1: Added the code (It works on the default emulator,pixel 3, but not on another) The logs are just to see if the code is doing what it should.
DatabaseReference database, newRef;
protected void onCreate(Bundle savedInstanceState) {
(...)
database = FirebaseDatabase.getInstance().getReference();
newRef = database.child(option).child(strSelectedYear).child(strSelectedMonth).child(strSelectedDay);
newRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot d: dataSnapshot.getChildren()) {
hours.remove(d.getKey());
Log.i("hAI FRAAA", d.getKey());
}
String msg = "";
for (int j = 0;j<hours.size();j+=1)
msg += " " + hours.get(j);
Log.i("Free hours", msg);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Solution
You'll need to do some more troubleshooting on your own to figure out what's going wrong. A few steps I would take:
- Stop ignoring possible errors and implement
onCancelled
. At its minimum that should bepublic void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }
. - Ensure the internet access works on the second emulator. So do other apps work?
- Enabling debug logging and check in the logcat output what is going on.
Answered By - Frank van Puffelen
Answer Checked By - Clifford M. (JavaFixing Volunteer)