Trying to save search files from DataBase

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.

Hello,

  1. Have you tested the where clause in Backendless console?
  2. Have you tried running your code in a debugger?

Regards,
Mark

Hey Mark,

  1. I haven’t, I don’t know how to…
  2. I’ve ran the code in debugging and find that the size would be 0, but my for doesn’t seem to run.

Thank you Mark,
Alex

See this article:

In the REST Console form there is Where field, this is where you can put your where clause to test it.

Mark

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.

Thank you Mark,
Alex

I mean, how can I turn this posts into a Post object?

Does the Group class have a field/property with the type of List<Posts>?

No, but it has a relation with the Posts class, 1:N. I’d like to retrieve the relation, or each post to a Post Object, or all the posts to an Array

What does this mean in terms of code? How does it have that relation at the code level?

I meant this Mark.
In Group Table:


As you can see theres a Relation to Posts.
In Posts Table:
There’s a relation FROM Group,

With this I want to know how can I get the objects posts related (via relation) to a certain group.

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.

Hello,
You can use Single Step Retrieval or Two Step retrieval to get the Post objects knowing the Group objectId. I assume the last example from this documentation is exactly what you need: https://backendless.com/docs/android/data_two_step_retrieval.html

LoadRelationsQueryBuilder<BackendlessUser> loadRelationsQueryBuilder; 
loadRelationsQueryBuilder = LoadRelationsQueryBuilder.of( Group.class );
loadRelationsQueryBuilder.setRelationName( "posts" );

String groupObjectId = // removed for brevity
Backendless.Data.of( Group.class ).loadRelations( groupObjectId,
        loadRelationsQueryBuilder,
        new AsyncCallback<List<Posts>>()
        {
          @Override
          public void handleResponse( List<Posts> posts )
          {
          }
          @Override
          public void handleFault( BackendlessFault fault )
          {
          }
        } 
);

Regards,
Olha

1 Like

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?

what does the crash error message say? Is there any helpful information?

I’m sending a print.

No matter what getArguments goes first, since the Asyncallback isn’t yet finished it gets null values.

Hi @Alexandre_Coutinho,

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.

Does it make sense?

Regards,
Mark

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.

1 Like