添加到另一个用户阵列Parse Android

时间:2015-07-13 20:21:01

标签: android arrays parse-platform

在Parse中,我在User表中有另一列称为'Friends',它是一个数组。从我当前的用户,我正在尝试添加到另一个用户的朋友阵列。

我试过了:

//buddy = a string of the other person's username
//to add to CURRENT user's array (this is working)
UserList.user.addUnique("Friends", buddy);
UserList.user.saveEventually();

    //this is to add to another user's array (NOT WORKING)
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("username", buddy);
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> objects, ParseException e) {
            if (e == null) {
                // The query was successful.
                for (ParseObject friend : objects) {
                    friend.addUnique("Friends",UserList.user.getUsername());
                    friend.saveEventually();
                }
            } else {
                // Something went wrong.
                Log.d("Username", "Error: " + e.getMessage());
            }
        }
    });

2 个答案:

答案 0 :(得分:4)

在将数据传递到云的java中编写方法,并通过名称调用云上的函数:

// the passes to the cloud is stored on HashMap
HashMap<String, Object> params = new HashMap<String, Object>();
// user's name to add the buddy to it
params.put("username", username);    
// buddy's name to pass to the cloud    
params.put("buddy", buddy);
ParseCloud.callFunctionInBackground("addBuddyToFriendsList", params, new FunctionCallback<String>(){
    public void done(String result, ParseException e){
    // e==null means no errors in running the function
       if(e==null){
          System.out.println(result);
       }else{
          System.out.println(e.getMessage());
       }
    }
});

好的,现在让我们编写要在云中使用的函数(假设您知道如何在云上部署函数):

<强> [EDITED]

Parse.Cloud.define("addBuddyToFriendsList", function (request, response){
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);    
query.find({
    success: function(user) {
        // query.find returns an array.....            
        user[0].addUnique("Friends", request.params.buddy);
        user[0].save(null, {
           success: function(success){
              response.success();
           }, 
           error: function (error){
               response.error(error);
           }
        });
        }, 
        error: function(object, error) {}
    });
});

答案 1 :(得分:3)

试试这个:

ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> objects, ParseException e) {
        if (e == null) {
            // The query was successful.
            for (ParseObject friend : objects) {
                friend.addUnique("Friends",UserList.user.getUsername());
                friend.saveEventually();
            }
        } else {
            // Something went wrong.
            Log.d("Username", "Error: " + e.getMessage());
        }
    }
});

我尝试以不同的方式调用该类,然后我将其称为普通类。通常这种方式适合我。 出于安全原因,“ParseUser”类型是有限的。