如何在Android中使用parse顺序运行代码?

时间:2015-07-14 22:17:51

标签: android parse-platform cloud countdown

我正在尝试从解析中获取记录。我的解析表包含一系列指针;我在编写解析查询时遇到了困难,所以我首先在ArrayList中保存指针数组,现在我创建一个for循环来执行查询;对于每个循环迭代,我想从解析中获取记录并更新本地数据库,然后与下一次迭代相同。但这会产生一些不同的问题。解析getInBackground不能按顺序工作....我的外部for循环完全执行然后调用解析方法,因为我遇到了在本地数据库中保存值的问题。

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();
        }
         */
}

现在,问题是我的外部循环一个接一个地执行,然后在循环结束之后,然后我的parse方法从解析中带来数据...因为它只是在db中更新记录而不是最后一个组id。 ..并没有针对第一个groupId插入记录 如何解决这个问题?我使用过这种技术,因为我没有编写查询来使用解析来获取指针数组的对象结果。

0 个答案:

没有答案