5

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) {

            }
        });
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
yeahman14
  • 75
  • 1
  • 6

4 Answers4

2

You'll need to do some more troubleshooting on your own to figure out what's going wrong. A few steps I would take:

  1. Stop ignoring possible errors and implement onCancelled. At its minimum that should be public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }.
  2. Ensure the internet access works on the second emulator. So do other apps work?
  3. Enabling debug logging and check in the logcat output what is going on.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for the answer but unfortunately it is still not working. I've implemented onCancelled and ensured that emulator has internet acces. The thing is that, i think the listener is triggered but it takes a lot of time for this emulator to get the data, just like the person who posted this: [link](https://stackoverflow.com/questions/50195049/firebase-android-works-on-the-emulator-but-not-on-the-device). I've also tried the answers people gave there but im really stuck. – yeahman14 Aug 02 '20 at 15:03
  • Did you also enable debug logging and check where the time is going, as I said in step 3? – Frank van Puffelen Aug 02 '20 at 15:14
  • 1
    I did the logging debug and the app is working now. The problem was caused by a memtrack error so i had to uninstall the app from the emulator and install it again. I feel so dumb and spent like 3 hours working on this thing but at least it's working. Thank you very much! – yeahman14 Aug 02 '20 at 15:27
0

I've been dealing with this issue for a few days. Finally connecting my physical android device to computer fixed the issue for me. I guess the emulators internet connection is not as healthy as Firestore expects. The cause of the problem was the emulator for me.

kimono
  • 31
  • 4
0

I hade same problem with latest Android Emulator Version, I just replaced with previews version and fixed my problem.

  1. Go to Android SDK
  2. Make Copy of Emulator
  3. Download previews version of Android Emulator from Emulator download archives
  4. Copy Emulator folder inside Android SDK
  5. Copy package.json from old Emulator and replace version with new version: <revision><major>31</major><minor>1</minor><micro>4</micro></revision> That's it.
KAKHA13
  • 305
  • 1
  • 3
  • 14
0

I have the same issue. In my case with Firebase Remote Config and Crashlytics. On the emulator is stops after FirebaseApp initialization. (API 28, Pixel 2). enter image description here

On the physical device it continues as expected. enter image description here

And just as I am writing the emulator continued and initialized the Firebase services. It took 7 minutes and several attempts (without any interaction from my side).

See the log: enter image description here

S. Gissel
  • 1,788
  • 2
  • 15
  • 32