【发布时间】:2019-06-11 06:21:02
【问题描述】:
我正在运行一个 addValueEventListener 到我的 Firebase 数据库,方法是运行一个按钮的 onClick。它使用用户数据检索数据快照,然后运行一个 for 循环,将变量分配给数据点,以便将它们保存到 sqlite 数据库中。最后一步是检查是否有一个 firebaseStorage url 表明有一个保存的图像作为给定“洞”的用户数据的一部分,如果是,则必须将其转换为字节数组。如果没有,则应用程序将字节数组定义为 null,然后在这两种情况下,应用程序都应将数据 .put 到数据库中。
我发现 for 循环似乎运行不规律,即没有按顺序完成它的过程。此外,在大多数情况下,即使数据快照中有明确的数据,90% 的时间也不会将其保存到变量中。最后,在某些情况下,虽然有一个 greenURL,但该应用程序既没有 .onSuccess 也没有 .onFailure 监听器。
感觉这是一个与onDataChange 是异步的事实相关的时间问题。我试图设计它,使其在过程完成之前不会继续,但也许我错了。这是我的代码:
String holeNumbers[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"};
SharedPreferences preferences = getApplicationContext().getSharedPreferences("MyPreferences", MODE_PRIVATE);
String selectedCourse = preferences.getString("selectedCourse","");
myDb = FirebaseDatabase.getInstance().getReference();
myDb.child("UserNotesData").child("Users").child(uid).child(selectedCourse).child(selectedNotes).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (String x:holeNumbers) {
String hole = "Hole"+x ;
String holeNumberNotes = x;
System.out.println("Hole at beginning of DownloadUserNotesData for loop: "+ x);
if (dataSnapshot.child(hole).child("ApproachIdealMiss").getValue() != null) {
approachIdealMiss = dataSnapshot.child(hole).child("ApproachIdealMiss").getValue().toString();
} else {
approachIdealMiss = "";
}
if (dataSnapshot.child(hole).child("ApproachNotes").getValue() != null) {
approachNotes = dataSnapshot.child(hole).child("ApproachNotes").getValue().toString();
} else {
approachNotes = "";
}
String greenImageURL;
if (dataSnapshot.child(hole).child("GreenImageURL").getValue() != null) {
greenImageURL = dataSnapshot.child(hole).child("GreenImageURL").getValue().toString();
} else {
greenImageURL = "N/A";
}
if (dataSnapshot.child(hole).child("GreenNotes").getValue() != null) {
greenNotes = dataSnapshot.child(hole).child("GreenNotes").getValue().toString();
} else {
greenNotes = "";
}
if (dataSnapshot.child(hole).child("TeeIdealMiss").getValue() != null) {
teeIdealMiss = dataSnapshot.child(hole).child("TeeIdealMiss").getValue().toString();
} else {
teeIdealMiss = "";
}
if (dataSnapshot.child(hole).child("TeeNotes").getValue() != null) {
teeNotes = dataSnapshot.child(hole).child("TeeNotes").getValue().toString();
} else {
teeNotes = "";
}
if (dataSnapshot.child(hole).child("TeeShot").getValue() != null) {
teeShot = dataSnapshot.child(hole).child("TeeShot").getValue().toString();
} else {
teeShot = "";
}
if (dataSnapshot.child(hole).child("goZone1X").getValue() != null) {
goZone1X = dataSnapshot.child(hole).child("goZone1X").getValue().toString();
} else {
goZone1X = "";
}
if (dataSnapshot.child(hole).child("goZone1Y").getValue() != null) {
goZone1Y = dataSnapshot.child(hole).child("goZone1Y").getValue().toString();
} else {
goZone1Y = "";
}
if (dataSnapshot.child(hole).child("goZone2X").getValue() != null) {
goZone2X = dataSnapshot.child(hole).child("goZone2X").getValue().toString();
} else {
goZone2X = "";
}
if (dataSnapshot.child(hole).child("goZone2Y").getValue() != null) {
goZone2Y = dataSnapshot.child(hole).child("goZone2Y").getValue().toString();
} else {
goZone2Y = "";
}
if (dataSnapshot.child(hole).child("noZone1X").getValue() != null) {
noZone1X = dataSnapshot.child(hole).child("noZone1X").getValue().toString();
} else {
noZone1X = "";
}
if (dataSnapshot.child(hole).child("noZone1Y").getValue() != null) {
noZone1Y = dataSnapshot.child(hole).child("noZone1Y").getValue().toString();
} else {
noZone1Y = "";
}
if (dataSnapshot.child(hole).child("noZone2X").getValue() != null) {
noZone2X = dataSnapshot.child(hole).child("noZone2X").getValue().toString();
} else {
noZone2X = "";
}
if (dataSnapshot.child(hole).child("noZone2Y").getValue() != null) {
noZone2Y = dataSnapshot.child(hole).child("noZone2Y").getValue().toString();
} else {
noZone2Y = "";
}
if(!greenImageURL.equals("N/A")) {
StorageReference greenStorageRef = storage.getReferenceFromUrl(greenImageURL);
final long ONE_MEGABYTE = 1024 * 1024;
greenStorageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
helperDB.insertUserNotesData(x, approachIdealMiss, approachNotes, greenImageURL, bytes,
greenNotes, teeIdealMiss, teeNotes, teeShot, goZone1X, goZone1Y, goZone2X, goZone2Y, noZone1X, noZone1Y, noZone2X, noZone2Y);
System.out.println("(greenImageSuccess) hole# to be written in db is: " + x);
System.out.println("(greenImageSuccess) TeeNotes to be written in db are: " + approachNotes);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
System.out.println("(greenImage was not blank but failed to be changed to bytes) hole# to be written in db is: "+ x);
}
}); }
else {
byte[] emptyByte = new byte[0];
helperDB.insertUserNotesData(x, approachIdealMiss, approachNotes, greenImageURL, emptyByte,
greenNotes, teeIdealMiss, teeNotes, teeShot, goZone1X, goZone1Y, goZone2X, goZone2Y, noZone1X, noZone1Y, noZone2X, noZone2Y);
System.out.println("(greenImageBlank) hole# to be written in db is: "+ x);
System.out.println("(greenImageBlank) TeeNotes to be written in db are: "+approachNotes);
}
}
loadingPercent = loadingPercent + 30;
loadingProgressBar.setProgress(loadingPercent, true);
System.out.println("loadingProgress at DownloadUserNotesData: " + loadingPercent.toString());
if (loadingPercent==100) {
Intent intent = new Intent (courseSelect.this, PlayNav.class);
startActivity(intent);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这里是控制台的相关(ish)打印,我试图用它来帮助调试:
V/FA: Processing queued up service tasks: 4
D/EGL_emulation: eglMakeCurrent: 0xa7805780: ver 3 0 (tinfo 0xa78039e0)
I/System.out: (greenImageSuccess) hole# to be written in db is: 2
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 3
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 1
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 6
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 11
I/System.out: (greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 8
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 12
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 10
I/System.out: (greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is: 14
(greenImageSuccess) TeeNotes to be written in db are:
W/Google Maps Android API: Deprecation notice: In a future release, indoor will no longer be supported on satellite, hybrid or terrain type maps. Even where indoor is not supported, isIndoorEnabled() will continue to return the value that has been set via setIndoorEnabled(), as it does now. By default, setIndoorEnabled is 'true'. The API release notes (https://developers.google.com/maps/documentation/android-api/releases) will let you know when indoor support becomes unavailable on those map types.
I/System.out: (greenImageSuccess) hole# to be written in db is: 13
(greenImageSuccess) TeeNotes to be written in db are:
I/System.out: (greenImageSuccess) hole# to be written in db is
【问题讨论】:
标签: android arrays image firebase firebase-storage