我无法从Firestore数据库中读取数据。我可以写它,尽管有时数据会在Firestore控制台上出现(非常晚)。 而且我可以删除一条记录,尽管它只是在执行写操作时从Firestore控制台删除。
这是我犯的一些愚蠢的简单错误,但我找不到。缓冲区?设置?我不知道:-(
firestore似乎延迟了响应,但始终无法达到以下要求:
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful())
....
logcat行下面...
我正在使用Java / Android应用
预先感谢您的帮助
,, E/PlayMissionActivity: sleep 2 <== ,, E/PlayMissionActivity: sleep 2 <== ,, W/Firestore: (0.6.6-dev) [OnlineStateTracker]: Could not reach Firestore backend. ,, E/PlayMissionActivity: sleep 2 <==
我的代码(只是示例的副本)
public class MyPlay extends AppCompatActivity {
FirebaseFirestore dbRef = null;
protected void onCreate(Bundle savedInstanceState) {
{
initDB();
getDocument();
int q=0;
while (q<10) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
q += 1;
Log.e(LOG_TAG, "sleep 2 " + "<==");
}
}
public void initDB() {
dbRef = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
dbRef.setFirestoreSettings(settings);
}
public void getDocument() {
DocumentReference docRef = dbRef.collection("users").document("123");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "----------------------------> DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "----------------------------> No such document");
}
} else {
Log.d(TAG, "----------------------------> get failed with ", task.getException());
}
}
});
}
}
app / build.gradle:
compile 'com.google.firebase:firebase-core:16.0.1'
compile 'com.google.firebase:firebase-firestore:17.0.2'
compile 'com.google.code.gson:gson:2.8.4'
Firestore规则:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Firestore数据:
users/123/born=1815
users/123/first="Ada"
users/123/last="Lovelace"
答案 0 :(得分:0)
解决了这个问题,这的确是根本。 基本上我不理解线程,事件等的概念。 当线程(事件)正在运行时,正在读取firestore db的线程正在等待完成主线程。 (伪代码,当onCreate完成时)
也许我可以解释得更好,我只是个老朋友
那么什么是行不通的(伪代码android活动):
onCreat{
object = readFirestore
print object.id <== this line will give you an error
}
readFirestore() {
read id
onSucces
object = document.toObject()
}
那什么工作正常:
onCreat{
object = readFirestore
}
readFirestore() {
read id
onSucces
object = document.toObject()
print object.id <== this line will print your result
}