对于firebase,你如何让@exclude工作?

时间:2016-08-25 04:39:58

标签: android firebase firebase-realtime-database

我有一个简单的User类。用户具有关注者的arrayList,其中每个关注者是用户对象。现在,我创建用户对象1和用户对象2.我将用户对象1添加到用户对象2的follwers数组,反之亦然。因此,现在用户对象2具有作为用户对象2的跟随者,其具有跟随者用户对象1.

我不能为我的生活找出当我发送用户对象1发送到我的firebase数据库时firebase继续崩溃的原因。我把@Exclude放在getter和arrayList上似乎没什么用。我的firebase安全性必须改变吗?因为如果我将用户对象1与用户对象2作为跟随者放在一起,并确保用户对象2没有用户对象1作为跟随者,那么它的工作原理。但是,我希望用户1关注用户2,反之亦然。

这是我的主要活动代码:

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRef = new Firebase("Fire base reference...");
        dummyUser();
    } 

private void dummyUser()
{
    UserFinal finalDummyUser = new UserFinal();
    finalDummyUser.setUserName("chris");
    finalDummyUser.setLocation("Netherlands");

    UserFinal finalDummyUser2 = new UserFinal();
    finalDummyUser2.setUserName("Andie");
    finalDummyUser2.setLocation("Germany");
    finalDummyUser2.addFollower(finalDummyUser);
    finalDummyUser.addFollower(finalDummyUser2);
    mRef.child("users").child("user3").setValue(finalDummyUser);
}

这是我的UserFinal类:

public class UserFinal
{
    private String userName;
    private String location;
    private int userID;
    //Arrays
    private ArrayList<UserFinal> userFollowers = new ArrayList<>();

    //Empty Constructor
    public UserFinal()
    {

    }
    //adders
    public void addFollower(UserFinal follower)
    {
        userFollowers.add(follower);
    }

    //setters
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    public void setLocation(String location)
    {
        this.location = location;
    }
    public void setUserID(int id)
    {
        this.userID = id;
    }
    public void setUserFollowers(ArrayList<UserFinal> followers)
    {
        this.userFollowers = followers;
    }

    //getters
    public String getUserName()
    {
        return userName;
    }
    public String getLocation()
    {
        return location;
    }
    public int getUserID()
    {
        return userID;
    }
    @Exclude
    public ArrayList<UserFinal> getUserFollowers()
    {
        return userFollowers;
    }
}

这是我的堆栈跟踪:

08-24 19:07:26.549 23384-23384/com.daprlabs.swipedeck E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.daprlabs.swipedeck, PID: 23384
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daprlabs.swipedeck/com.daprlabs.swipedeck.ActivityCenter}: com.firebase.client.FirebaseException: Failed to parse to snapshot
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: com.firebase.client.FirebaseException: Failed to parse to snapshot
at com.firebase.client.Firebase.setValueInternal(Firebase.java:357)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.daprlabs.swipedeck.ActivityCenter.dummyUser(ActivityCenter.java:165)
at com.daprlabs.swipedeck.ActivityCenter.onCreate(ActivityCenter.java:122)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5525) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: java.lang.IllegalArgumentException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.Arra

08-24 19:07:26.577 23384-23394/com.daprlabs.swipedeck W/art: Suspending all threads took: 32.173ms

1 个答案:

答案 0 :(得分:1)

堆栈跟踪中com.firebase.client的存在表明您正在使用传统的Firebase SDK 2.X进行构建。在旧版SDK中,要使用的注释为@JsonIgnore。它在新的9.X SDK中被@Exclude取代。

此更改也在Upgrade Guide

中进行了描述