解析:单个saveInBackground调用的重复记录

时间:2015-11-18 08:38:49

标签: java android parse-platform

我遇到了一个事件,其中一个条目的完整副本被写入数据库(同一个用户评论出现两次)。他们有不同的对象ID,但在其他方面完全相同。完成发布的速度比平常慢,并且只发生过几十次评论,所以我想说这是在saveInBackground调用期间的Parse问题。即便如此,我预计像Parse这样的服务会更加强大。作为我第一次使用Android,我也不能确定我的结果没有错。有帮助吗?还有任何批评吗?这是用户点击评论提交按钮时调用的方法:

private void submitComment() {

    String text = commentText.getText().toString().trim();
    Intent intent = getIntent();
    String ID = intent.getStringExtra("imageID");
    String parentID = intent.getStringExtra("parent");

    // Set up a progress dialog
    final ProgressDialog loadingDialog = new ProgressDialog(CommentSubmitActivity.this);
    loadingDialog.setMessage(getString(R.string.publishing_comment));
    loadingDialog.show();

    Comment comment = new Comment();
    comment.setText(text);
    comment.setUser((ParseUser.getCurrentUser()));
    if (ID.equals("@child")) {
        comment.setParent(parentID);
        comment.setImage("@child");

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Comment");
        query.getInBackground(parentID, new GetCallback<ParseObject>() {
            public void done(ParseObject parentComment, ParseException e) {
                if (e == null) {
                    int numChild = parentComment.getInt("numChild");
                    parentComment.put("numChild", ++numChild);
                    parentComment.saveInBackground();
                } else {
                    Log.d("numChild: ", "error");
                }
            }
        });
    } else {
        comment.setImage(ID);
        comment.put("numChild", 0);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("ImageUpload");
        query.getInBackground(ID, new GetCallback<ParseObject>() {
            public void done(ParseObject image, ParseException e) {
                if (e == null) {
                    int numComments = image.getInt("numComments");
                    image.put("numComments", ++numComments);
                    image.saveInBackground();
                } else {
                    Log.d("numComments: ", "error");
                }
            }
        });

    }

    comment.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                loadingDialog.dismiss();
                finish();
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我遇到了类似你的问题。 我创建了一个应用程序,用户可以创建帐户并添加照片和对象列表(在我的情况下是朋友)。 一旦我测试它,用户就创建了两次。 我查看了我的代码,我的怀疑与异步调用有关。 我看到您在应用程序中使用异步解析api,因此没有代码片段等待响应并阻止其余操作。 您无法控制解析服务器何时响应。 我做了什么我只是将所有同步请求放入我的自定义异步代码(Android中的AsyncTask)。

希望我的回答以某种方式满足您的期望。