实时到.push()的firestore等效项

时间:2019-04-20 04:51:33

标签: java android firebase firebase-realtime-database google-cloud-firestore

我正在将我的实时数据库应用程序转换为firestore。我不知道的问题是我在使用.push()的应用程序中,firestore与此等效吗?我在下面发布了我的代码,仅供参考。抱歉,如果含糊不清,不知道还包括什么

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_activity);

    firebaseFirestore = FirebaseFirestore.getInstance();
    mMessageDoc = firebaseFirestore.document("Messages");

    // mFirebaseDatabase = FirebaseDatabase.getInstance();
    // mMessagedatabaseReference = mFirebaseDatabase.getReference().child("messages");


    mFirebaseStorage = FirebaseStorage.getInstance();
    mChatPhotosStorageReference = mFirebaseStorage.getReference().child("chat_photos");

    // Initialize references to views
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mMessageListView = (ListView) findViewById(R.id.messageListView);
    mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
    mMessageEditText = (EditText) findViewById(R.id.messageEditText);
    mSendButton = (Button) findViewById(R.id.sendButton);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    final String current_uid = mCurrentUser.getUid();

    // Initialize progress bar
    mProgressBar.setVisibility(ProgressBar.INVISIBLE);

    //Initialize message ListView and its adapter
    List<FriendlyMessage> friendlyMessages = new ArrayList<>();
    mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
    mMessageListView.setAdapter(mMessageAdapter);

    // ImagePickerButton shows an image picker to upload a image for a message
    mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
        }
    });



    // Enable Send button when there's text to send
    mMessageEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.toString().trim().length() > 0) {
                mSendButton.setEnabled(true);
            } else {
                mSendButton.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

    // Send button sends a message and clears the EditText
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString());
            mMessagedatabaseReference.push().setValue(friendlyMessage);
            // Clear input box
            mMessageEditText.setText("");
        }
    });

    mChildEventListner = new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);
            mMessageAdapter.add(friendlyMessage);
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

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

        }
    };
    mMessagedatabaseReference.addChildEventListener(mChildEventListner);

    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), current_uid, null);
            mMessagedatabaseReference.push().setValue(friendlyMessage);

            // Clear input box
            mMessageEditText.setText("");
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {

        Uri selectedImageUri = data.getData();

        final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());

        photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String downloadUrl = uri.toString();
                        Log.d("tag", downloadUrl);
                        FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);
                        mMessagedatabaseReference.push().setValue(friendlyMessage);
                        //Toast.makeText(ChatActivity.this, ), Toast.LENGTH_LONG).show();

                    }
                });
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

我没有详细研究您的代码,但您可能会认为……的“等效”

实时数据库push()方法,“创建对自动生成的子位置的引用

Firestore add()方法,“将具有指定数据的新文档添加到集合中,自动为其分配文档ID 。”