Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

android - Confirm internet data connection before making Firestore Query

I have the following situation:

The application has the option to like the posts of a user. And also has a Dislike option.

It is working properly when it is on a wifi or mobile network (with online connection).

Problem:

When the network is mobile, it is often connected but has no data transmission and does not work. It is a geographical and operator problem in certain locations in the country.

So it's taking a long time for the Like option to have its effect for either Like or DisLike.

Code:

@Override
    public void onLikeSelected(final DocumentSnapshot like) {

        if (checkConnection()){


            final String idCurrentUser = mCurrentUser.getUid();

            Task<QuerySnapshot> query = mDb.collection("Postagens").document(like.getId()).collection("Likes").whereEqualTo("id_userLike", idCurrentUser)
                    .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot documentSnapshots) {

                            if(documentSnapshots.size() == 0) {

                                Map<String, Object> dado_usuario = new HashMap<>();
                                dado_usuario.put("id_userLike", mCurrentUser.getUid());
                                dado_usuario.put("timestamp", FieldValue.serverTimestamp());

                                mDb.collection("Postagens").document(like.getId()).collection("Likes").document(idCurrentUser).set(dado_usuario).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {



                                    }
                                });



                            } else { //utilizando else



                                mDb.collection("Postagens").document(like.getId()).collection("Likes").document(idCurrentUser).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {



                                    }
                                });

                            }


                        }
                    });



        } else {

            Toast.makeText(getActivity(), "Not Connection", Toast.LENGTH_SHORT).show();
        }

}

 public  boolean checkConnection() {
        boolean conectado;
        ConnectivityManager conectivtyManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected()) {
            conectado = true;
        } else {
            conectado = false;
        }
        return conectado;
}

How can I do this when the mobile connection is bad or weak

I do not know if it would be the case of handling the data connection or changing the treatment of the Like (onLikeSelected()) option in the Firestore Database.

Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...