How to run code sequentially with parse, in Androi

2019-07-22 15:23发布

I am trying to get records from parse. My table in parse contains an array of pointers; I was facing difficulties to write parse query, so I first save array of pointers in an ArrayList, now I make a for loop to execute the query; for each loop iteration, I want to get records from parse and update local db then same as for next iterations. But this is creating some different problems. parse getInBackground is not working sequentially.... my outer for loop completely executes then parse method called due to which I am facing problems to save values in local db.

public void insertGroupsInDB(ArrayList<TempGroupClass> temp) 
{
    Log.d(TAG,"insertGroupsInDB: temp size:"+temp.size());
    for(int i = 0;i<temp.size();i++) `//my target is to make inner query run till no of loop times and for each iterations inner parse query will run and then insert records in db against outer insertion group` 
    {
        Groups grp = new Groups();
        grp.setGroupTitle(temp.get(i).getGroupTitle());
        grp.setGroupType(temp.get(i).getGroupType());
        grp.setParseObjectId(temp.get(i).getParseObjectId());
        long groupinsert  = (YouinDatabase.getInstance(context)).addGroup(grp,context);
        //}
        /*try
          {
          final CountDownLatch latch = new CountDownLatch(1);*/
        if(groupinsert != -1)
        {
            //now insert friends
            //long friendInsertId = YouinDatabase.getInstance(context).addFriend();
            //now get friends from members id 

            Log.d(TAG,"groups inserted successfully:"+groupinsert);
            final ArrayList<Integer> list = new ArrayList<Integer>();

            if(temp.get(i).getFriendObjectIdList().size() > 0)
            {        
                for(int j =0;j<temp.get(i).getFriendObjectIdList().size();j++)
                {
                    Log.d(TAG," >>>>>>>>>>>>>>>friend objectId>>>>>>>>>>>>>>:"+temp.get(i).getFriendObjectIdList().get(j));

                    ParseQuery<ParseUser> query = ParseUser.getQuery();
                    query.whereContainedIn("objectId",temp.get(i).getFriendObjectIdList());
                    query.findInBackground(new FindCallback<ParseUser>() {

                            @Override
                            public void done(List<ParseUser> arg0,
                                    ParseException arg1) {
                            // TODO Auto-generated method stub
                            if(arg1 == null)
                            {
                            //Log.d(TAG,"arg0 size:"+arg0.size());
                            if(arg0.size() >0)
                            {
                            for(int i = 0;i<arg0.size();i++)
                            {
                            Log.d(TAG,"arg0.size():"+arg0.size());
                            Friend f = new Friend();
                            f.setUsername(arg0.get(0).getString("username"));
                            f.setParseObjectId(arg0.get(0).getObjectId());
                            f.setHasAdded(false);
                            boolean userAlreadyExist = YouinDatabase.getInstance(context).checkUserExistInFriendTable(arg0.get(0).getString("username"));
                            long friendInsertId = -1;
                            ArrayList<Integer> list = new ArrayList<Integer>();
                            int friendid;

                            if(!userAlreadyExist)
                            {
                                // Log.d(TAG,"friend Already not exist :"+userAlreadyExist);
                                friendInsertId = YouinDatabase.getInstance(context).addFriend(f);

                                list.add(YouinDatabase.getInstance(context).findFriendIdOfLatestRecord());
                                friendid = YouinDatabase.getInstance(context).findFriendIdOfLatestRecord();
                            }

                            else
                            {
                                //Log.d(TAG,"friend Already exist :"+userAlreadyExist);
                                //list.add(YouinDatabase.getInstance(context).getFriendIdFromFriendName(arg0.get(0).getString("username")));
                                friendid = YouinDatabase.getInstance(context).getFriendIdFromFriendName(arg0.get(0).getString("username"));

                            }
                            //  Log.d(TAG,"list size 1 :"+list.size());
                            int latestGroupInsertId = YouinDatabase.getInstance(context).findGroupIdOfLatestRecord();


                            long id =  YouinDatabase.getInstance(context).addFriendInConnection(friendid,latestGroupInsertId);
                            //now update user setHasAdded 
                            long updateFriendTable = -1;
                            if(id != -1)
                            {
                                updateFriendTable = YouinDatabase.getInstance(context).updateFriendTable(friendid);
                            }

                            Log.d(TAG,">>>>updated friend id information:>>>>");

                            if(updateFriendTable != -1)
                            {
                                Friend friendDetails = YouinDatabase.getInstance(context).getFriendDetailsFromFriendId(friendid);
                                Log.d(TAG,"friend name:"+friendDetails.getUsername());
                                Log.d(TAG,"friend:"+friendDetails.getParseObjectId());
                                Log.d(TAG,"friend added :"+friendDetails.isHasAdded());
                                Log.d(TAG,"groupId:"+latestGroupInsertId);
                            }
                            //YouinDatabase.getInstance(context).get
                            }
                            Log.d(TAG,"list size 2"+list.size());
                            }
                            }
                            else
                            {
                                Log.d(TAG,"arg1 != null:"+arg1.getMessage());
                            }
                            }
                    });
                }
                // Log.d(TAG,"list size:"+list.size());

            }
            //latch.countDown();
        }
        /*latch.await();
          }

          catch (IllegalArgumentException e) {
          e.printStackTrace();
          } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
         */
}

Right now, the problem is that my outer loop executes twice one after another and then after the loop ends, then my parse method brings data from parse ...due to which it's only updating record in db against last group id ...and it's not inserting records against first groupId How to resolve this issue? I have used this technique because I failed to write query to get object result of array of pointers using parse.

1条回答
时光不老,我们不散
2楼-- · 2019-07-22 16:03

You could use find() which should do a synchronous operation which might hold up your ui. I have not used parse yet so i dont know. Or you can set up something like below. Remove the outer and check conditions in your callback to determine when to launch the next query.

private int j = 0;
private int loopnumber = temp.size();
ArrayList<TempGroupClass> temp; //setup temp somewhere else

private void doQuery() {
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereContainedIn("objectId",temp.get(i).getFriendObjectIdList());
    query.findInBackground(new FindCallback<ParseUser>() {

        @Override
        public void done(List<ParseUser> arg0,
                         ParseException arg1) {
            // TODO Auto-generated method stub
            if(arg1 == null)
            {
            ...

            ...
            else
            {
                Log.d(TAG,"arg1 != null:"+arg1.getMessage());
            }
            //at the end call the same method to start a query if the loop conditions have not been reached.
            if(++i < loopnumber) {
                doQuery();
            }
        }
    });
}
}
查看更多
登录 后发表回答