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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…