prevent重复条目parse.com [英] Prevent duplicate entries parse.com

查看:121
本文介绍了prevent重复条目parse.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Parse.com作为我的后端,虽然似乎有一种方法, saveInBackgroundWithBlock ,以prevent重复的条目。它似乎并不存在,在Android上。我想只上传唯一条目,但不能想出一个办法这样做。

I'm using Parse.com as my backend and while there seems to be a method, saveInBackgroundWithBlock, to prevent duplicate entries. It doesn't appear to exist on Android. I'd like to upload only unique entries but can't figure out a way to do so.

我唯一能想到的是查询然后插入如果条目不存在,但是这是在做两倍的网络电话,我觉得它需要。

The only thing I can think of is to query then insert if the entry doesn't exist, but that's doing twice as many network calls and I feel like it needs to.

感谢

推荐答案

正如我所提到的意见前,我曾面临同样的问题。最后写一个查询来查找现有对象,然后只保存不存在的。如下图所示。

As I had mentioned in the comment earlier, I had faced the same problem. Ended up writing a query to find the existing objects and then save only the non-existing ones. Like below.

//假设你有ParseObjects..this清单列表包含现有的以及新的对象。

//Say you have a list of ParseObjects..this list contains the existing as well as the new objects.

List<ParseObject> allObjects = new ArrayList<ParseObject>();
allObjects.add(object); //this contains the entire list of objects.

您想找出现有的使用领域说的ID。

You want to find out the existing ones by using the field say ids.

//First, form a query
ParseQuery<ParseObject> query = ParseQuery.getQuery("Class");
query.whereContainedIn("ids", allIds); //allIds is the list of ids

List<ParseObject> Objects = query.find();  //get the list of the parseobjects..findInBackground(Callback) whichever is suitable

for (int i = 0; i < Objects.size(); i++)
      existingIds.add(Objects.get(i).getString("ids"));

List<String> idsNotPresent = new ArrayList<String>(allIds);
idsNotPresent.removeAll(existingIds);

//Use a list of Array objects to store the non-existing objects
List<ParseObject> newObjects = new ArrayList<ParseObject>();

for (int i = 0; i < selectedFriends.size(); i++) {
     if (idsNotPresent.contains(allObjects.get(i).getString(
                        "ids"))) {
     newObjects.add(allObjects.get(i)); //new Objects will contain the list of only the ParseObjects which are new and are not existing.
    }
}

//Then use saveAllInBackground to store this objects

ParseObject.saveAllInBackground(newObjects, new SaveCallback() {

    @Override
    public void done(ParseException e) {
    // TODO Auto-generated method stub
    //do something
        }
    });

我也尝试过使用 beforeSave 方法对 ParseCloud 。正如你可能知道,在保存对象之前调用此方法在 ParseCloud ,是理想做出所需的任何验证。但是,它并没有完全办好。让我知道如果你需要从 ParseCloud code的东西。

I had also tried using beforeSave method on ParseCloud. As you may know, before saving the objects this method is called on the ParseCloud and is ideal to make any validation required. But, it didn't quite run well. Let me know if you need something from the ParseCloud code.

希望这有助于!

这篇关于prevent重复条目parse.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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