Reputation: 1960
I'm trying to retrieve a information from a parse object but the strings I store them in keep equaling null.
Here is how I saved the object
// get current user
ParseObject studentClasses = new ParseObject("StudentClasses");
// register their periods into database
studentClasses.put("student_id", ParseUser.getCurrentUser());
studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);
// save the information into database
studentClasses.saveInBackground();
It saves it perfectly fine my database. The student_id is a pointer to the user and the rest of the columns are strings.
I want to retrieve all those strings and put them in an array when I query parse for them it doesn't work
Here is my query
// check if a user is not cached
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null)
{
// prompt user to Register screen
// create intent to start activity
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// start new activity
startActivity(intent);
// stop current activity
finish();
}
// query database for user's classes
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
query.whereEqualTo("student_id", ParseUser.getCurrentUser());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
@Override
public void done (ParseObject parseObject, ParseException e)
{
if (e == null)
{
// retrieved the object
userClasses[PERIOD1] = parseObject.getString("first_period");
userClasses[PERIOD2] = parseObject.getString("second_period");
userClasses[PERIOD3] = parseObject.getString("third_period");
userClasses[PERIOD4] = parseObject.getString("fourth_period");
userClasses[PERIOD5] = parseObject.getString("fifth_period");
userClasses[PERIOD6] = parseObject.getString("sixth_period");
userClasses[PERIOD7] = parseObject.getString("seventh_period");
}
else
{
// failed lookup. Do something here
Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
}
}
});
I looked at the parse docs and it looks like it should work but it doesn't save the strings
Any help or comments is appreciated thanks!
EDIT: I showed more of my code for the query part to show there is a current user
Upvotes: 2
Views: 229
Reputation: 1960
After getting some sleep then doing more research and tinkering I found the problem. The problem isn't in my code; it's in my network... well not entirely. You see my code runs ... at well run-time (As fast as my phone can process it); however, the method
query.findInBackground(new FindCallback<ParseObject>()
runs asynchronously meaning that it does not run at the speed of my other code. Which makes sense if you think about it because it has to send and wait for a response from a database. That's why when I made a toast inside it, the data was there in the string but when I tried to make a toast a few lines later, outside of the method, the data was not there. It was null.
Here's an example. I make a toast at the end of on create with this code
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise and set toolbar as actionbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// initialize nav bars
initNavBars();
// initialize drawer layout
NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
// initialize nav drawer
navDrawer = (DrawerLayout) findViewById(R.id.drawer);
initNavDrawer(navDrawer);
// initialize layout manager for recycler view
RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);
// initialize data for all classes before setting adapter
initClassData(); // <---- MY PARSE QUERY IS IN THIS METHOD
// set the adapter for recycler view
RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);
// initialize recycler view elements
RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
// add layout manager to recycler view
mainRecyclerView.setLayoutManager(mainLayoutManager);
// add adapter to recycler view
mainRecyclerView.setAdapter(mainAdapter);
Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show(); // <----- HERE IS MY TOAST
}
When I run it, the toast is empty because the string is still null, but if I run it with this code
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise and set toolbar as actionbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// initialize nav bars
initNavBars();
// initialize drawer layout
NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
// initialize nav drawer
navDrawer = (DrawerLayout) findViewById(R.id.drawer);
initNavDrawer(navDrawer);
// initialize layout manager for recycler view
RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);
// initialize data for all classes before setting adapter
initClassData(); // <---- PARSE QUERY IS STILL IN THIS METHOD
// set the adapter for recycler view
RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);
// initialize recycler view elements
RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
// add layout manager to recycler view
mainRecyclerView.setLayoutManager(mainLayoutManager);
// add adapter to recycler view
mainRecyclerView.setAdapter(mainAdapter);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
// Do something after 5s = 5000ms
Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show();
}
}, 5000);
}
In this example I forced the toast to wait five seconds to allow
query.getFirstInBackground(new GetCallback<ParseObject>()
to finish querying the database and when I run it, the toast displays the string correctly.
So if your program relies on a parse query to get important data, you have to structure your code in a way to allow for a second or two to pass to let the parse query return.
Or you could alternatively store the data locally and then you can use the database as a backup for the data and check to make sure it is the same every time the user wants to switch it or somethings.
Thank you Ajay and Rasika I would still be trying to figure why it was not working if it weren't you.
Upvotes: 0
Reputation: 129
Here what you need to understand is in your code studentClasses.saveInBackground();
is async call and you need to query inside SaveCallback. Then you can assure that the saved data is in the database when your query runs.
You got null
data because you query before the data is saved in parse.
ParseObject studentClasses = new ParseObject("StudentClasses");
// register their periods into database
studentClasses.put("student_id", ParseUser.getCurrentUser());
studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);
// save the information into database
studentClasses.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
query.whereEqualTo("student_id", ParseUser.getCurrentUser());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
@Override
public void done (ParseObject parseObject, ParseException e)
{
if (e == null)
{
// retrieved the object
userClasses[PERIOD1] = parseObject.getString("first_period");
userClasses[PERIOD2] = parseObject.getString("second_period");
userClasses[PERIOD3] = parseObject.getString("third_period");
userClasses[PERIOD4] = parseObject.getString("fourth_period");
userClasses[PERIOD5] = parseObject.getString("fifth_period");
userClasses[PERIOD6] = parseObject.getString("sixth_period");
userClasses[PERIOD7] = parseObject.getString("seventh_period");
}
else
{
// failed lookup. Do something here
Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
}
}
});
} else {
// myObjectSaveDidNotSucceed();
}
}
});
Hope this helps
Upvotes: 0
Reputation: 430
Well try when getting content to use the function
parseObject.get("the_name_of_the_column")
Which actually should work and works fine for me
Upvotes: 0
Reputation: 2023
Here i am assuming that, there is data stored in your parse database.. and it works fine.. so to retrieve it try following..
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
query.whereEqualTo("student_id", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseObject>()
{
@Override
public void done (List<ParseObject> list, ParseException e)
{
if (e == null)
{
for(int i=0; i < list.size(); i++) {
userClasses[PERIOD1] = list.get(i).getString("first_period");
userClasses[PERIOD2] = list.get(i).getString("second_period");
userClasses[PERIOD3] = list.get(i).getString("third_period");
userClasses[PERIOD4] = list.get(i).getString("fourth_period");
userClasses[PERIOD5] = list.get(i).getString("fifth_period");
userClasses[PERIOD6] = list.get(i).getString("sixth_period");
userClasses[PERIOD7] = list.get(i).getString("seventh_period");
}
}
else
{
Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
}
}
});
hope it helps!
Upvotes: 2