无法在firebase中保存新数据

时间:2016-10-29 09:27:46

标签: android firebase firebase-realtime-database

我正在尝试在节点Visitors下的firebase中保存新数据。我已经在该节点上设置了一些规则。

"Visitor":{
  "$userId":{
    ".write": "auth != null && $userId === auth.uid",
      ".read": " auth.uid != null"
  }
},

在我的应用中

FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();

if(mFirebaseUser == null){

}else{
  String mUserId = mFirebaseUser.getUid();
  DatabaseReference vRef = mDatabase.getReference(MyFirebaseConstants.DATABASE_Visitor);
  DatabaseReference newVisitorRef = vRef.child(mUserId).push();
  String visitorKey = newVisitorRef.getKey();

 Visitor visitor = new Visitor()//POJO
  // other few setters

  visitor.setId(visitorKey);
  newVisitorRef.setValue(property);

我的访问者pojo扩展了RealmObject并实现了Parcelable

但是我收到了以下错误

 java.lang.StackOverflowError
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.google.android.gms.internal.zzamy$zza.zzci(Unknown Source)
 at com.google.android.gms.internal.zzamy.zzca(Unknown Source)
 at com.google.android.gms.internal.zz
 //and many more like this

我的访客pojo

      public class Visitor extends RealmObject implements Parcelable{

       @PrimaryKey
      private long timeStamp;
      private String visitorId;
      private String phoneNumber="";
      private String profileUri="";

      public Visitor(){

      }

    protected Visitor(Parcel in) {
    timeStamp = in.readLong();
    visitorId = in.readString();
    phoneNumber = in.readString();
    profileUri = in.readString();
    }

  public static final Creator<Visitor> CREATOR = new Creator<Visitor>() {
    @Override
    public Visitor createFromParcel(Parcel in) {
      return new Visitor(in);
    }

    @Override
    public Visitor[] newArray(int size) {
      return new Visitor[size];
    }
  };

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(timeStamp);
    dest.writeString(visitorId);
    dest.writeString(phoneNumber);
    dest.writeString(profileUri);
  }
      public void setTimeStamp(long timeStamp){
      this.timeStamp = timeStamp;
      }

      public long getTimeStamp(){
       return this.timeStamp;
      }

      public void setVisitorId(String visitorId){
      this.visitorId = visitorId;
      }

      public void setPhoneNumber(String phoneNumber){
       this.phoneNumber = phoneNumber;
      }

      public Uri getProfileUri() {
        return Uri.parse(profileUri);
      }
      public void setProfileUri(Uri profileUri) {
        this.profileUri = profileUri.toString();
      }

    }

1 个答案:

答案 0 :(得分:4)

我知道我的pojo中的问题在哪里

public Uri getProfileUri() {
    return Uri.parse(profileUri);
  }

  public void setProfileUri(Uri profileUri) {
    this.profileUri = profileUri.toString();
  }

Realm支持Uri类型的getter和类型为Uri的setter作为参数,但firebase数据库不支持

所以在我的pojo中,我改为

public String getProfileUri() {
    return profileUri;
  }
  public void setProfileUri(String profileUri) {
    this.profileUri = profileUri;
  }