【发布时间】:2020-08-02 12:31:47
【问题描述】:
我的应用正在检查某个数据是否在 Firebase 实时数据库中,并将 addListenerForSingleValueEvent 添加到数据库引用中。我开始该项目的模拟器工作正常并且可以完美地检索数据,但是当我更改模拟器(假设我切换到 PIXEL 3XL)时,监听器不起作用。我在另一个 stackoverflow 问题中看到有人遇到了同样的问题,并且数据库检索了数据,但经过了很长时间。有谁知道为什么会这样?我是否应该继续使用默认模拟器进行开发而不关心实时数据库无法在另一个模拟器上运行?你能解释一下为什么会这样吗?
我在清单中使用它:
...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>...
分级
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(模块)
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'
}
编辑 1: 添加了代码(它适用于默认模拟器,像素 3,但不适用于另一个)日志只是为了查看代码是否在做它应该做的事情。
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) {
}
});
}
【问题讨论】:
-
@a_local_nobody 刚刚添加了代码。
-
可能是因为您使用的是免费版本的 Firebase?它有时会停止工作;)
-
@LeoLeontev :) 我真的不这么认为,因为它总是在默认模拟器上工作。
-
其他模拟器用的android sdk是什么?
-
@PeterHaddad 我对此很陌生。我希望sdk和模拟器的API版本是一样的。两者(一个工作的和一个不工作的)都是 29
标签: android firebase firebase-realtime-database