Values of override method not reflecting on main variables
up vote
0
down vote
favorite
I am trying to change values of my class from an override method as shown below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");
}
return;
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}
}
}
return;
}
}
From the analyse()
method i am calling the getEntity()
method, the override method is working fine and is changing the value of identified
and entityIdentified
accordingly but, when the method is completed the updates are not reflected on the variables.
I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?
I have tried implementing threads as well, to make sure the method getEntity()
gets completed before returning to the main, but the problem persists.
java android firebase firebase-realtime-database
New contributor
add a comment |
up vote
0
down vote
favorite
I am trying to change values of my class from an override method as shown below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");
}
return;
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}
}
}
return;
}
}
From the analyse()
method i am calling the getEntity()
method, the override method is working fine and is changing the value of identified
and entityIdentified
accordingly but, when the method is completed the updates are not reflected on the variables.
I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?
I have tried implementing threads as well, to make sure the method getEntity()
gets completed before returning to the main, but the problem persists.
java android firebase firebase-realtime-database
New contributor
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to change values of my class from an override method as shown below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");
}
return;
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}
}
}
return;
}
}
From the analyse()
method i am calling the getEntity()
method, the override method is working fine and is changing the value of identified
and entityIdentified
accordingly but, when the method is completed the updates are not reflected on the variables.
I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?
I have tried implementing threads as well, to make sure the method getEntity()
gets completed before returning to the main, but the problem persists.
java android firebase firebase-realtime-database
New contributor
I am trying to change values of my class from an override method as shown below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");
}
return;
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}
}
}
return;
}
}
From the analyse()
method i am calling the getEntity()
method, the override method is working fine and is changing the value of identified
and entityIdentified
accordingly but, when the method is completed the updates are not reflected on the variables.
I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?
I have tried implementing threads as well, to make sure the method getEntity()
gets completed before returning to the main, but the problem persists.
java android firebase firebase-realtime-database
java android firebase firebase-realtime-database
New contributor
New contributor
edited yesterday
Frank van Puffelen
217k25361384
217k25361384
New contributor
asked yesterday
Samiirah Aujub
13
13
New contributor
New contributor
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago
add a comment |
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// the logic here will have to also be moved into the Firebase call
}
}
Note that, the parts that required the check on identified
have all been moved to the onDataChanged
method.
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into theonDataChanged
method, where all your views can be updated with arunOnUIThread
call.
– Cool Guy CG
10 hours ago
add a comment |
up vote
0
down vote
Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// the logic here will have to also be moved into the Firebase call
}
}
Note that, the parts that required the check on identified
have all been moved to the onDataChanged
method.
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into theonDataChanged
method, where all your views can be updated with arunOnUIThread
call.
– Cool Guy CG
10 hours ago
add a comment |
up vote
0
down vote
The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// the logic here will have to also be moved into the Firebase call
}
}
Note that, the parts that required the check on identified
have all been moved to the onDataChanged
method.
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into theonDataChanged
method, where all your views can be updated with arunOnUIThread
call.
– Cool Guy CG
10 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// the logic here will have to also be moved into the Firebase call
}
}
Note that, the parts that required the check on identified
have all been moved to the onDataChanged
method.
The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:
public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
}
public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// the logic here will have to also be moved into the Firebase call
}
}
Note that, the parts that required the check on identified
have all been moved to the onDataChanged
method.
answered yesterday
Cool Guy CG
68048
68048
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into theonDataChanged
method, where all your views can be updated with arunOnUIThread
call.
– Cool Guy CG
10 hours ago
add a comment |
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into theonDataChanged
method, where all your views can be updated with arunOnUIThread
call.
– Cool Guy CG
10 hours ago
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
21 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the
onDataChanged
method, where all your views can be updated with a runOnUIThread
call.– Cool Guy CG
10 hours ago
That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the
onDataChanged
method, where all your views can be updated with a runOnUIThread
call.– Cool Guy CG
10 hours ago
add a comment |
up vote
0
down vote
Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
add a comment |
up vote
0
down vote
Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.
Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.
answered 16 hours ago
Ashutoshg
555
555
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
add a comment |
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
12 hours ago
add a comment |
Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.
Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.
Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.
Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238024%2fvalues-of-override-method-not-reflecting-on-main-variables%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
yesterday
when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday
You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
yesterday
Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago