代码之家  ›  专栏  ›  技术社区  ›  Paradox

将实时数据库databasereference.addListenerForsingleValueEvent转换为Cloud FireStore

  •  -1
  • Paradox  · 技术社区  · 8 年前

    我想转换以下实时数据库代码以使用FireBase:

    private void setupFirebaseAuth(){
            Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
    
            mAuth = FirebaseAuth.getInstance();
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            myRef = mFirebaseDatabase.getReference();
    
            mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
    
                    if (user != null) {
                        // User is signed in
                        Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
    
                        myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                //1st check: Make sure the username is not already in use
                                if(firebaseMethods.checkIfUsernameExists(username, dataSnapshot)){
                                    append = myRef.push().getKey().substring(3,10);
                                    Log.d(TAG, "onDataChange: username already exists. Appending random string to name: " + append);
                                }
                                username = username = append;
    
                                //add new user to the database
    
                                //add new user_account_settings to the database
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
    
                            }
                        });
    
                    } else {
                        // User is signed out
                        Log.d(TAG, "onAuthStateChanged:signed_out");
                    }
                    // ...
                }
            };
        }
    

    其中变量定义如下:

    //firebase
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseMethods firebaseMethods;
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference myRef;
    

    我可以重用其中的大部分,除非数据库定义如下:

    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference myRef;
    

    它将是:

    private FirebaseFirestore db;
    

    与FireStore的数据库引用对应的是什么,因为稍后,我不知道如何将myref.addListenerForsingleValueEvent调整为CloudFireStore

    1 回复  |  直到 8 年前
        1
  •  2
  •   Frank van Puffelen    8 年前

    最接近于 addListenerForSingleValueEvent 在云中,FireStore是 get() .

    这个 Firestore documentation on getting data 包含更完整的示例:

    DocumentReference docRef = db.collection("cities").document("SF");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
    

    我建议检查文档并尝试一下。如果在转换的特定步骤中卡住了,请显示您尝试过的操作。