0

I want to show all object from every Relation in following Query. But it still empty. I tried with the this Code, but it did not work. I hope someone can help me. Thanks in advance

mData = new ArrayList<>();
        ParseQuery<ParseObject> receiver = ParseQuery.getQuery("Group");
        receiver.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> feed, ParseException e) {
                if (e == null) {
                    mData = feed;
                    for (int i = 0; i < mData.size(); i++) {
                        ParseRelation<ParseObject> relation = mData.get(i).getRelation("Data");
                        ParseQuery<ParseObject> query = relation.getQuery();
                        query.findInBackground(new FindCallback<ParseObject>() {
                            @Override
                            public void done(List<ParseObject> objects, ParseException e) {
                                rData = objects;
                                Log.e("APP", String.valueOf(rData.size()));
                                Log.d("APP", "mData" + mData.size());

                                if (mSwipeStack.getAdapter() == null) {
                                    mAdapter = new SwipeAdapter(getContext(), rData);
                                    mSwipeStack.setAdapter(mAdapter);
                                    mAdapter.notifyDataSetChanged();

                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        public void run() {
                                            Log.d("mAdapter", "Loaded");
                                        }
                                    }, 4000);
                                    //mSwipeStack.setListener(getActivity());
                                    Log.i("APP", "We found messages!");
                                }
                            }
                        });
                    }

                }

            }
        });

Sorry for my bad english :)

Manuel
  • 14,274
  • 6
  • 57
  • 130
Loyal
  • 497
  • 1
  • 5
  • 9

1 Answers1

1

I suggest you to use pointers and not relations (in your case) and i will explain why:

It is not best practice to run findInBackground in a loop because if your dataset (mData) size will be large you will execute mData.size calls to the server and this is not a best practice and also not scalable. I suggest you to use Pointer Arrays (you can read more about it here) by using arrays you will need only one service call to get all the data to your client (this way your solution will be more scalable and much more simple)

So your new solution (if you will decide to go with it of course :)) will be the following:

  • Group collection
  • Data collection
  • One to many from Group to collection (just read in docs how it can be done)

And then your code will look like the following:

EDIT1 - use getList

ParseQuery<ParseObject> receiver = ParseQuery.getQuery("Group");
reciever.include("data");
receiver.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> feed, ParseException e) {
               // in here you can access all the data which under group in the following way:

                 for (int i=0;i < feed.size();i++){
                    ParseObject obj = feed.get(i);
                    // here you have all the data collection 
                    List<ParseObject> dataItems = obj.getList("data");
                    // now you can loop on dataItems
                 }

            }
});

Please note there is a chance that my code is not 100% accurate but i hope you got the idea.

Good Luck :)

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
  • Hey, thanks for your answer. I have one problem, i hope you can help me. When i put `List dataItems = obj.get("data");` i get **Incompatible Types Required Java.util.List Found Java.lang.Object** – Loyal Dec 16 '16 at 15:17
  • Sorry you should use getList and not get (i will update my answer) but please make sure that your names are correct.. – Ran Hassid Dec 16 '16 at 15:18
  • I get **java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference ** – Loyal Dec 16 '16 at 15:33
  • that's because maybe you trying to access to wrong name.. just look at your model (you can also look inside mongo to see how it is saved.. ) – Ran Hassid Dec 16 '16 at 15:34
  • can you check if in all the results you get null (maybe via debugger or in code) ? – Ran Hassid Dec 16 '16 at 15:44
  • You mean `Log.d(TAG, dataitems.size());` Or what do you mean – Loyal Dec 16 '16 at 15:46
  • When i check the result with `Log.d(TAG, String.valueOf(obj.getList("data")));` I get **[com.parse.ParseObject@dbcdfb7]** – Loyal Dec 16 '16 at 15:54
  • so it looks good maybe you need to check if it's not null just to make sure you have entries there.. – Ran Hassid Dec 16 '16 at 15:54
  • I tried what you said it worked, but i have one problem. `if(obj.getList("data") != null){ mData = obj.getList("data");` i get only one data. – Loyal Dec 16 '16 at 16:25
  • sorry, i dont understand what you mean. can you give me a example? @RanHassid – Loyal Dec 16 '16 at 17:24
  • You have a list of groups under each group you have list of data so in order to get all the data items under your groups you need to iterate on the groups and access to data array of each group – Ran Hassid Dec 16 '16 at 17:25