从Firestore获取和更新数据 - 问题

时间:2018-01-28 13:56:46

标签: java android firebase google-cloud-firestore

对于我的应用,我有很多场合需要检索和写入数据到Firestore。 我能够编写新数据并获取和显示数据。我也可以在一个类中覆盖数据,但是尽管使用了相同的方法,但现在又遇到了另一个问题。

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseUser currUser = FirebaseAuth.getInstance().getCurrentUser();
    DocumentReference userDocRef = db.collection("Users").document(currUser.getEmail());
    //access current values saved under this user
    userDocRef.get().addOnSuccessListener(new 
    OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            User currentUser = documentSnapshot.toObject(User.class);
            warmups_skipped = currentUser.getWarmupsSkipped()+1;
            Log.d(TAG, "DocumentSnapshot successfully retrieved! " + warmups_skipped);

        }
    });

我可以在logCat中看到局部变量正在被更改。到目前为止都很好。 然后我在以下之后立即调用此方法:

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseUser currUser = FirebaseAuth.getInstance().getCurrentUser();
    DocumentReference userDocRef = db.collection("Users").document(currUser.getEmail());
    Map<String, Object> update = new HashMap<>();
    update.put(WARMUPSSKIPPED, getWarmups_skipped());
        userDocRef
                .set(update, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "Document has been saved");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "Document could not be saved");
        }
    });
    }

此处我还会显示TAG消息,但FireStore中没有任何更改。我在另一个类中使用完全相同的第二种方法,我不检查,已经在线的内容,但只是覆盖它。 真的很感激帮助!

1 个答案:

答案 0 :(得分:0)

好吧我终于明白了。显然我不能将这两个动作分开,所以我将第二个方法放在第一个方法中,现在它似乎有效。但是,并不完全确定为什么变量是类变量,应该可以从两种方法访问。如果其他人有这个问题,现在就是它对我有用的方式:

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseUser currUser = FirebaseAuth.getInstance().getCurrentUser();
    final DocumentReference userDocRef = db.collection("Users").document(currUser.getEmail());
    //access current values saved under this user
    userDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            User currentUser = documentSnapshot.toObject(User.class);
            warmups_skipped = currentUser.getWarmupsSkipped()+1;
            Map<String, Object> update = new HashMap<>();
            update.put(WARMUPSSKIPPED, warmups_skipped);
            userDocRef
                    .set(update, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "Document has been saved");
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "Document could not be saved" +e.toString());
                }
            });
            Log.d(TAG, "DocumentSnapshot successfully retrieved! " + warmups_skipped);
        }
    });