问题描述
我想将数据从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 (将#修改为@)