如何运行C $ C依次解析$,这是Android? [英] How to run code sequentially with parse, in Android?

查看:111
本文介绍了如何运行C $ C依次解析$,这是Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从parse得到记录。我在解析表包含指针数组;我面临的困难写分析查询,所以我先救指针数组中的一个的ArrayList ,现在我做一个for循环来执行查询;每个循环迭代,我想从解析记录和更新本地数据库,然后为下一个迭代相同。但是,这是创造一些不同的问题。解析 getInBackground 不是连续工作....我的循环外执行完全解析,然后叫方法,由于它,我面临的问题,保存在本地分贝值。

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

现在,问题是,我外环相继执行两次一个,然后循环结束后,再我的解析方法从parse ...因为它只有在与去年组ID数据库更新记录带来的数据。 ..和它不是针对插入第一记录的groupId
如何解决这个问题?我已经使用这种技术,因为我没有写查询来获取使用解析指针数组对象的结果。

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.

推荐答案

您可以使用的 找到() 应该做这可能会托起你的用户界面同步操作。我没有用解析尚未所以我不知道。或者你也可以建立类似下面。取出外,检查情况在回调,以确定何时启动下一个查询。

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

这篇关于如何运行C $ C依次解析$,这是Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆