Firebase出现StackOverflow错误,同时在我的Android项目上使用setValue()方法

时间:2019-01-18 08:13:03

标签: android firebase firebase-realtime-database stack-overflow

我存储用户参加并创建的每个事件。我想,使用.setValue(user)时,存储在User中的列表会导致StackOverflow错误。

public class User {

        //Field
        private String name;
        private String email;
        private List<Event> attending_events = new ArrayList<Event>();
        private List<Event> created_events = new ArrayList<Event>();
    }

没有用户参加任何事件,他们的列表为空,我可以毫无问题地使用.setValue(user)。但是,一旦将事件添加到用户的Attings_events中,就会出现StackOverflow错误。

    public boolean joinEvent(){

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference events = database.getReference().child( "events");
        DatabaseReference users = database.getReference().child( "users");

        String type = event.getType();
        DatabaseReference event_type = events.child( type);

     event.setNumberOfCurrentParticipants(event.getNumberOfCurrentParticipants() ++);

        event.setRateOfParticipants();
        event.add( user);

        FirebaseClass.updateEvent( event_type, event);

        user.getAttending_events().add( event);


        users.child(user.getName()).setValue( user); // StackOverflow
}

错误消息是:

java.lang.StackOverflowError: stack size 8MB
    at java.lang.reflect.Method.invoke(Native Method)
    at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:58)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:237)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:191)
    at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
    at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:120)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:237)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:191)
    at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
    at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:120)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:237)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:191)
    at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
    at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:120)
    ...

我该如何解决?

我的Event类,不包括getter和setter。

公共类活动{

//Properties
private String title;
private String place;
private String date;
private String deadline;
private int numberOfCurrentParticipants;
private int numberOfParticipants;
private String description;
private String type;
private String userName;
private double rateOfParticipants;
private ArrayList<User> user_list;
private String key;

/**
 * Empty Constructor
 */
public Event(){
    user_list = new ArrayList<User>();
}

public Event( String title, String place, String date, String deadline, int numberOfParticipants, String description, String userName) {
    this.title = title;
    this.place = place;
    this.date = date;
    this.deadline = deadline;
    this.numberOfParticipants = numberOfParticipants;
    this.description = description;
    this.userName = userName;
    numberOfCurrentParticipants = 1;
    type = null;
    setRateOfParticipants();
    user_list = new ArrayList<User>();
}


public void add( User user)
{
    user_list.add( user);
}
public void remove( User user)
{
    int index = -1;
    for(int i = 0; i < user_list.size(); i ++)
    {
        if( user_list.get( i).equals( user))
            index = i;
    }
    user_list.remove( index);
}

还有我的FirebaseClass.updateEvent()方法。 (这也会产生stackoverflow错误):

public static void updateEvent( final DatabaseReference ref, final Event event)
{
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> events = dataSnapshot.getChildren();
            for(DataSnapshot snapshot : events){
                Event temp = snapshot.getValue(Event.class);
                if(temp.getTitle().equals(event.getTitle()) && temp.getDescription().equals(event.getDescription())) {
                    snapshot.getRef().setValue(event); // StackOverflow

                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

[已解决]

我意识到我试图将用户存储在事件的user_list中,并且所有用户都具有Attinging_list,该表也将事件存储在user_list中。因此,存在列表的递归分支。然后,这会导致android studio抛出stackoverflow错误。

1 个答案:

答案 0 :(得分:0)

我希望您的Event类包含一个User属性,然后该属性不仅会添加用户,还会添加该用户的事件。由于用户所在的事件也意味着用户所在的事件,因此您将陷入无限循环。因为事件添加了用户,而用户又添加了事件,用户又添加了用户,事件也添加了...,直到堆栈溢出。

为防止堆栈溢出,请删除一个事件中一个用户或一个用户中一个事件的嵌套,或者不要通过@Exclude对其进行注释来将其写入数据库。