简体   繁体   English

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

[英]StackOverflow error with Firebase, while using setValue() method on my Android Project

I store each event that user attends and has created.我存储用户参加和创建的每个事件。 I suppose, the lists that are stored in User cause StackOverflow error while using .setValue( user).我想,存储在 User 中的列表在使用 .setValue(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>();
    }

Without users attending any events, their lists are empty and I can use .setValue(user) without any problem.没有用户参加任何活动,他们的列表是空的,我可以毫无问题地使用 .setValue(user) 。 However, once I add events to users' attending_events, I get StackOverflow error.但是,一旦我将事件添加到用户的参加事件中,我就会收到 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
}

And the Error message is:错误消息是:

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)
    ...

How do I solve this?我该如何解决这个问题?

My Event class, excluded getters and setters.我的 Event 类,排除了 getter 和 setter。

public class Event {公共类事件{

//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);
}

And my FirebaseClass.updateEvent() method.还有我的 FirebaseClass.updateEvent() 方法。 (This also gives stackoverflow error) : (这也会导致计算器溢出错误):

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) {

        }
    });
}

[SOLVED] [解决了]

I realized that I was trying to store users inside event's user_list and all users have attending_list which also stores events with user_list.我意识到我正在尝试将用户存储在事件的 user_list 中,并且所有用户都有参与列表,它也使用 user_list 存储事件。 Therefore, there are recursive branches of lists.因此,存在列表的递归分支。 Then, this causes android studio to throw stackoverflow error.然后,这会导致 android studio 抛出 stackoverflow 错误。

I expect that your the Event class to contains a User property, which then adds not just the user, but also that user's events.我希望您的Event类包含一个User属性,然后不仅添加用户,还添加该用户的事件。 Since an event that the user is at, also means that the user is at that event, you have an infinite loop.由于用户所在的事件也意味着用户在该事件中,因此您有一个无限循环。 Because the event adds the user, who in turn adds the event, which in turn adds the user, which adds the event,... until the stack overflows.因为事件添加了用户,谁又添加了事件,又依次添加了用户,又添加了事件,...直到堆栈溢出。

To prevent the stack overflow, either remove the nesting of either users in an event, or an event in users, or don't write it to the database by annotating it with @Exclude .为了防止堆栈溢出,要么删除事件中用户的嵌套,要么删除用户中的事件,或者不要通过使用@Exclude注释将其写入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM