Firebase数据无法立即获取

时间:2016-10-06 13:24:17

标签: android firebase-realtime-database realm

我不知道为什么当我尝试调用getFromRealm()时,我无法在第一时间检索它,以便获取我必须关闭的数据并重新打开应用程序。 我是以错误的方式做的吗?我试过使用Sqlite,但他们遇到了同样的问题。有人可以帮忙吗? 我的目标是立即获取数据并在现场检索。 以下是代码。

MainActivity:

public class MainActivity extends AppCompactActivity {
private static final String TAG = "MainActivity";
public String mName;
public String mEmail;
private Realm mRealm;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRealm = Realm.getInstance(this);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mAuth = FirebaseAuth.getInstance();

    saveToRealm();
    getFromRealm();
    Log.d(TAG, "Name: " + mName); // Here show null at first open the app
    Log.d(TAG, "Email: " + mEmail); // Here show null at first open the app
 }
}

这是将数据保存到Realm方法:

 public void saveToRealm() {
    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Users details = dataSnapshot.getValue(Users.class);
            Log.d(TAG, String.valueOf(dataSnapshot));

            String name = details.getName();
            String email = details.getEmail();

            details.setName(name);
            details.setEmail(email);

            mRealm.beginTransaction();
            mRealm.copyToRealmOrUpdate(details);
            mRealm.commitTransaction();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    };
    mDatabase.child("User")
            .child("Details")
            .addValueEventListener(postListener);
}

这是获取数据方法:

 public void getFromRealm() {
    RealmResults<Users> results =
            mRealm.where(Users.class).findAll();

    try{
        Users c = results.get(0);
        mName = c.getName();
        mEmail = c.getEmail();

       } catch (Exception e) {
        Log.e(TAG, String.valueOf(e));
    }
}

这是对象类:

public class Users extends RealmObject {

@PrimaryKey
private long id;

private String name;
private String email;

public Users() {
}

public void setId(long id) {
    this.id = id;
}

public long getId() {
    return id;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setEmail(String email) {
    this.email = email;
}

public String getEmail() {
    return email;
}

}

1 个答案:

答案 0 :(得分:1)

从你存储的地方检索数据......

public void saveToRealm() {
    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Users details = dataSnapshot.getValue(Users.class);
            Log.d(TAG, String.valueOf(dataSnapshot));

            String name = details.getName();
            String email = details.getEmail();

            details.setName(name);
            details.setEmail(email);

            mRealm.beginTransaction();
            mRealm.copyToRealmOrUpdate(details);
            mRealm.commitTransaction();

            getFromRealm();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    };
    mDatabase.child("User")
            .child("Details")
            .addValueEventListener(postListener);
}

因为从firebase数据获取数据需要花费几秒钟的时间,因此只在您存储时检索数据。

尝试以这种方式获取您的数据..

public void getFromRealm() { 
    RealmResults<Users> results = mRealm.where(Users.class).findAll();
        try {
               for(int i=0;i<results.size();i++){

                 mName = results.get(i).getName();
                 mEmail = results.get(i).getEmail();

            }
         } catch (Exception e) {
                Log.e(TAG, String.valueOf(e)); 
      } 
  }