So I’m trying to get posts from my database, including names, descriptions, and images, all this is already saved in my backendless but I’m really having a lot of trouble trying to retrieve this information and save it so I can use it or display it.
This is my code on that topic:
String whereClause = "groups.objectId = '" + groupObjectId + "'";
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause(whereClause);
//Encontrar na base de dados as publicações relacionadas com este grupo
Backendless.Persistence.of(Posts.class).find(queryBuilder, new AsyncCallback<List<Posts>>() {
@Override
public void handleResponse(List<Posts> response) {
int size = response.size();
Log.d("size", "size"+ size);
for( Posts p : response){
Posts pT = new Posts();
pT.setName(p.getName());
pT.setDescription(p.getDescription());
pT.setPostpic(p.getPostpic());
pT.setPropic(p.getPropic());
postToPass.add(pT);
}
Bundle b = new Bundle();
b.putParcelableArrayList("lstPosts", postToPass);
b.putString("groupId", groupObjectId);
FeedFrag feedFrag = new FeedFrag();
feedFrag.setArguments(b);
}
@Override
public void handleFault(BackendlessFault fault) {
Log.d("INFO","failed to search");
Toast.makeText(MainActivity.this, "Error on gathering posts", Toast.LENGTH_SHORT).show();
}
});
My postToPass gets a size of 0 when adding it to the bundle, so I’m getting a NullPointer. I just don’t know how to solve it and save the posts inside that array.
I’ve tested the whereClause finding it was wrong, but I’m not being able to do it. So I have a groups Table that contains the users and posts with relations to it. How can I get the all the posts of a given group? Is it with the whereClause? Because that was what I was trying to do.
My question is not about the schema in the database, I got that part. The question is how you structured it in the code, because that’s what you were asking about.
Well, I do the code I showed you in the beginning of my Main Activity where I use the FeedFrag on a ViewPager with a Fragment Manager, but the thing is that the WhereClause actually doesn’t work. I didn’t quite get what you mean by how I did structured it in the code, but not being able to get the posts, so I can’t save them into object and display them. Thanks to you I’ve found out that the WhereClause was the problem but still don’t know how to change it and get the relation from the groups with the posts.
That was the solution for the problem. But I have one last problem, I’m now able to get the posts I wanted, and I can use them. But because of the Asynchronous call the application crashes before getting the all the values it needs, only working if I pass it through debug step by step.
Any solution for this? Any way I can delay the main thread?
It seems to me the code tries to access the result of the invocation before it is available. You need to refactor the code so that the response processing starts only after you actually get the response.
Yes mark it does make sense! But how do I do it? I’ve fixed it by creating that asyncallback on the activity before my main one, but still… I’d like to have it the way I had, this is, in the Main Activity
One way to do this is to put the code that works with the response data into the handleResponse method. Another way to do this is to create a separate activity which works with the response data and dispatch an intent with the response into that activity.