如何解决如何在后台将数据从Firebase存储到会议室数据库中
我想将数据从firebase存储到本地数据库中,但是在尝试逻辑之后完成任务
逻辑1-
private void retrieveNotes(){
reference2.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot){
if(snapshot.getValue()!=null){
for(DataSnapshot dataSnapshot:snapshot.getChildren()){
note.setTitle(dataSnapshot.child("title").getValue(String.class));
note.setSubtitle(dataSnapshot.child("subtitle").getValue(String.class));
note.setNoteText(dataSnapshot.child("noteText").getValue(String.class));
note.setDateTime(dataSnapshot.child("dateTime").getValue(String.class));
note.setColor(dataSnapshot.child("color").getValue(String.class));
note.setImagePath(dataSnapshot.child("imagePath").getValue(String.class));
note.setId(Integer.parseInt((String.valueOf(dataSnapshot.child("id").getValue()))));
note.setWebLink(dataSnapshot.child("webLink").getValue(String.class));
@SuppressLint("StaticFieldLeak")
class SaveNoteTask extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids) {
NotesDatabase.getDatabase(getApplicationContext()).noteDao().insertNote(note);
return null;
}
}
new SaveNoteTask().execute();
}
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}else{
findViewById(R.id.progress_circular).setVisibility(View.GONE);
Toast.makeText(ProfileActivity.this,"No record Found...",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error){
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}
});
}
这种情况下只检索上一个笔记,但我想检索所有笔记并将其保存到房间数据库中。
逻辑2-
private void retrieveNotes(){
AsyncTask.execute(new Runnable(){
@Override
public void run(){
reference2.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot){
if(snapshot.getValue()!=null){
for(DataSnapshot dataSnapshot:snapshot.getChildren()){
note.setTitle(dataSnapshot.child("title").getValue(String.class));
note.setSubtitle(dataSnapshot.child("subtitle").getValue(String.class));
note.setNoteText(dataSnapshot.child("noteText").getValue(String.class));
note.setDateTime(dataSnapshot.child("dateTime").getValue(String.class));
note.setColor(dataSnapshot.child("color").getValue(String.class));
note.setImagePath(dataSnapshot.child("imagePath").getValue(String.class));
note.setId(Integer.parseInt((String.valueOf(dataSnapshot.child("id").getValue()))));
note.setWebLink(dataSnapshot.child("webLink").getValue(String.class));
NotesDatabase.getDatabase(getApplicationContext()).noteDao().insertNote(note);
}
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}else{
findViewById(R.id.progress_circular).setVisibility(View.GONE);
Toast.makeText(ProfileActivity.this,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error){
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}
});
}
});
}
在这种情况下,logcat中的错误消息-
无法访问主线程上的数据库,因为它可能长时间锁定UI。
逻辑3-
private void retrieveNotes(){
reference2.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot){
if(snapshot.getValue()!=null){
for(DataSnapshot dataSnapshot:snapshot.getChildren()){
note.setTitle(dataSnapshot.child("title").getValue(String.class));
note.setSubtitle(dataSnapshot.child("subtitle").getValue(String.class));
note.setNoteText(dataSnapshot.child("noteText").getValue(String.class));
note.setDateTime(dataSnapshot.child("dateTime").getValue(String.class));
note.setColor(dataSnapshot.child("color").getValue(String.class));
note.setImagePath(dataSnapshot.child("imagePath").getValue(String.class));
note.setId(Integer.parseInt((String.valueOf(dataSnapshot.child("id").getValue()))));
note.setWebLink(dataSnapshot.child("webLink").getValue(String.class));
NotesDatabase.getDatabase(getApplicationContext()).noteDao().insertNote(note);
}
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}else{
findViewById(R.id.progress_circular).setVisibility(View.GONE);
Toast.makeText(ProfileActivity.this,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error){
findViewById(R.id.progress_circular).setVisibility(View.GONE);
}
});
}
在这种情况下,也会出现相同的错误消息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。