TreeMap自动删除自身

时间:2019-05-16 23:36:24

标签: java android firebase firebase-realtime-database treemap

我的代码从Firebase实时数据库中检索布尔值。但是,这些值会自动删除,没有明显的原因。我不明白为什么会这样并且发疯。

代码应该像这样工作:创建视图,下载布尔值(如果存在),完成。

这似乎正在发生:创建视图,下载布尔值(如果存在),擦除它们,完成。

这是我的代码:

public class ACdayFragment extends Fragment {

//TAG
private static final String TAG = "ACdayFragment";

//Context
private Context context;

//Days for Booleans
Boolean Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;

//Firebase Stuff
FirebaseMethods firebaseMethods;
DatabaseReference userDatabase;
DatabaseReference availDatabase;
FirebaseAuth mAuth;

//Strings
String key = mAuth.getInstance().getUid();

//TreeMap
TreeMap<Integer, Boolean> dayMap = new TreeMap<>();


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_ac, container, false);

    mAuth = FirebaseAuth.getInstance();
    key = FirebaseAuth.getInstance().getUid();
    userDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(key);
    availDatabase = userDatabase.child("Days");


    //This method should populate my dayMap treemap
    getData();

    //But when I retrieve it here, it appears empty
    Log.e(TAG, "getData0: "+ dayMap);



    return view;
}

private void getData() {
    FirebaseUser user = mAuth.getCurrentUser();

    if (user != null) {
        availDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (getActivity() == null) {
                    return;
                }
                if (dataSnapshot.exists() && dataSnapshot.getChildrenCount()>0) {
                    Map<String, Boolean> map = (Map<String, Boolean>) dataSnapshot.getValue();

                    if (map.get("sunday")!= null) {
                        dayMap.put(0, true);
                    }
                    if (map.get("monday")!= null) {
                        dayMap.put(1, true);
                    }
                    if (map.get("tuesday")!= null) {
                        dayMap.put(2, true);
                    }if (map.get("wednesday")!= null) {
                        dayMap.put(3, true);
                    }if (map.get("thursday")!= null) {
                        dayMap.put(4, true);
                    }if (map.get("friday")!= null) {
                        dayMap.put(5, true);
                    }if (map.get("saturday")!= null) {
                        dayMap.put(6, true);
                    }


                    //This log does show me a populated dayMap
                    Log.e(TAG, "getData1: "+ dayMap );
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
    Log.d(TAG, "getData2: " + dayMap );
}
@Override
public void onStart() {
    super.onStart();
}
@Override
public void onStop() {
    super.onStop();
}
}

1 个答案:

答案 0 :(得分:1)

基本上,您需要了解以下两行代码之间会发生什么:

getData(); //Fire and Forget
//...
//The getData() method has requested data from Firebase but it takes sometime to return the data
//Meanwhile, this method execution doesn't wait for Firebase and hence the dayMap in the next line is not filled yet!
//...
Log.e(TAG, "getData0: "+ dayMap); //Does not wait for Firebase to return the data

getData()方法只是创建并添加一个包含ValueEventListener方法的onDataChange对象,当Firebase完成获取数据时,将首次调用此方法。请注意,每次更新数据时也会调用它。请参阅ValueEventListener

的文档

我建议您先了解Synchronous and Asynchronous execution。另外,您必须learn about listenerscallbacks

P.S:@NewAppDeveloping不要讨厌自己。只需学习基础知识即可。

更新:

解决此问题的最简单方法是通过更新onDataChange方法来对布尔树映射进行某些操作,如下所示:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    if (getActivity() == null) {
        return;
    }
    ....
    ....//Your existing code: Get the booleans and create the treemap
    ....
    //This log does show me a populated dayMap
    Log.e(TAG, "getData1: "+ dayMap );

    ....//Do what you intend to here like filling the view/ updating the view, etc.
}

onCreateView必须返回一个视图,如果您正在等待Firebase的数据,那是不对的。如上所述,您必须返回空视图,然后在收到数据后填充它。