【发布时间】:2017-07-26 23:13:43
【问题描述】:
我知道这个问题可能看起来是重复的,我已经阅读了十个关于同一件事的其他帖子,但我找不到问题
我的活动中有这个方法:
public void saveResponse(final Response studentResponse, final Content content)
fb.getReference("...").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
new Thread(new Runnable() {
@Override
public void run() {
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("end_date", studentResponse.end_date);
responseMap.put("start_date", studentResponse.start_date);
responseMap.put("time", studentResponse.time);
responseMap.put("points", studentResponse.points);
responseMap.put("max_points", studentResponse.max_points);
responseMap.put("selected_options", studentResponse.selected_options);
if (!TextUtils.isEmpty(studentResponse.free_text))
responseMap.put("free_text", studentResponse.free_text);
DataSnapshot contentRef = dataSnapshot.child("/sections/" + currentSection + "/sections/" + currentSubsection + "/contents/" + content.id);
final int oldPoints = contentRef.hasChild("points") ? contentRef.child("points").getValue(int.class) : 0;
contentRef.getRef().setValue(responseMap);
contentRef.getRef().setPriority(ServerValue.TIMESTAMP);
DataSnapshot subSectionRef = dataSnapshot.child("/sections/" + currentSection + "/sections/" + currentSubsection);
long subSectionPoints = (subSectionRef.hasChild("points") ? subSectionRef.child("points").getValue(long.class) : 0) + studentResponse.points - oldPoints;
subSectionRef.child("points").getRef().setValue(subSectionPoints);
int indexOf = currentContents.indexOf(content) + 1;
if(indexOf > 0 && indexOf < currentContents.size()) {
CourseContent content = currentContents.get(indexOf);
subSectionRef.child("currentPosition").getRef().setValue(content.order);
}
DataSnapshot sectionRef = dataSnapshot.child("/sections/" + currentSection);
long sectionPoints = (sectionRef.hasChild("points") ? sectionRef.child("points").getValue(long.class) : 0) + studentResponse.points - oldPoints;
sectionRef.child("points").getRef().setValue(sectionPoints);
long coursePoints = (dataSnapshot.hasChild("points") ? dataSnapshot.child("points").getValue(long.class) : 0) + studentResponse.points - oldPoints;
dataSnapshot.child("points").getRef().setValue(coursePoints);
dataSnapshot.getRef().setPriority(MAX_SAFE_INTEGER - coursePoints);
int completed = 0;
for (DataSnapshot sect : dataSnapshot.child("sections").getChildren()) {
for (DataSnapshot subSect : sect.child("sections").getChildren()) {
int currPos = subSect.hasChild("currentPosition") ? subSect.child("currentPosition").getValue(int.class) : 0;
completed += currPos;
}
}
double progress = totalContents > 0 ? (double) completed / (double) totalContents : 0;
dataSnapshot.child("progress").getRef().setValue(progress);
}
}.start();
}
...
});
}
在单击处理程序中,我调用此方法,然后更改片段(使用自定义动画)。
问题是,片段过渡并不平滑,它会冻结一点,如果我在可运行文件中注释所有内容,那么它会运行顺利。我也尝试过使用 AsyncTask 并且发生了同样的情况。
在 runnable 内部,我只是查询 dataSnapshot 及其子项,并设置一些值 (dataSnapshot.child("item").getRef().setValue(x))
另外一个奇怪的地方是,如果我在run()里面放了一个断点,它也能顺利运行。
【问题讨论】:
-
也许是 Android 处理程序而不是创建新线程? stackoverflow.com/questions/15136199/…
-
“当你想在 UI 线程中执行操作时,你应该使用 Handler.post()”,我不想在 UI 线程中执行它,这就是我创建另一个线程的原因。
-
请更新帖子以将代码包含在
run()方法中。 -
@qbix 在
run()method 中添加了代码 -
saveResponse()参数response未使用。这是您发布的代码中的错字吗?应该是studentResponse?currentSection、currentSubsection和currentContents声明在哪里?
标签: android multithreading firebase firebase-realtime-database