ios - 查询 PFInstallation 并从云代码添加到 channel 键

标签 ios parse-platform pfquery parse-cloud-code

我正在尝试将订阅添加到特定 PFUser 的所有 PFInstallations(对于一个人在 iPhone iPad 等上使用相同登录的情况)。我有他们所有使用该应用程序的 Facebook 好友的表格 View 。在选择行中的 friend 时,我希望它获取我的 objectId 并查询所有 PFInstallations 以获取所有 PFInstallations 的数组,其中键 usersObjectId 与 PFUser objectId 匹配。然后,我可以为每个 PFInstallations 的 channel 添加一个值。我有:

FriendArray *job = self.jobsArray[indexPath.row];
    PFQuery *query = [PFUser query];
//job.facebookid is the Facebook id for that particular user
//each PFUser has a fbId and for those who logged in with Facebook, their Facebook ID is stored here
    [query whereKey:@"fbId" equalTo:job.facebookid];
    PFUser *user = (PFUser *)[query getFirstObject];
//This gives me the PFUser whose fbId value matches the Facebook id for the row that was selected
    NSString *subscription = [@"User_" stringByAppendingString:user.objectId];
    PFUser *me = [PFUser currentUser];

    PFQuery *pushQuery = [PFInstallation query];
//Trying to get all PFInstallations that match the current user's usersObjectId, so I can add the value to each channel
    [pushQuery whereKey:@"usersObjectId" equalTo:[me objectId]];
    PFInstallation *allInstallations = (PFInstallation *)[pushQuery findObjects];
    [allInstallations addUniqueObject:subscription forKey:@"channels"];

不过,它告诉我不能直接查询 PFInstallation。我如何在云代码中做同样的事情?

最佳答案

好吧,经过几个小时的工作,我终于弄明白了,并认为会发布解决方案。如果您想编辑特定类型的所有 PFInstallation 条目(我通过始终将我的 PFUser objectId 作为新值放在 PFInstallation 上来执行此操作),您可以这样做。

云代码使用:

Parse.Cloud.define("subscribingAll", function(request, response) {
      Parse.Cloud.useMasterKey();

    var usersObjectId = request.params.usersObjectId;
      var newSubscription = request.params.newSubscription;


  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("usersObjectId", usersObjectId);

     pushQuery.find({
    success: function(results) {

      response.success(results);
    },
    error: function() {
      response.error("failed");
    }
  });
});

然后,在您的应用上,您需要一个 PFCloud 调用来处理所有这些。

[PFCloud callFunctionInBackground:@"subscribingAll"
                       withParameters:@{@"usersObjectId": [me objectId], @"newSubscription": subscription}
                                block:^(NSArray *theCount, NSError *error) {
                                    if (!error) {
                                        int i;
                                        for (i = 0; i < [theCount count]; i++) {
                                            PFInstallation *change = [theCount objectAtIndex:i];
                                            [change addUniqueObject:subscription forKey:@"channels"];
                                            [change saveInBackground];
                                        }

                                    }
                                }];

云代码返回一个数组,其中每个对象都是来自与查询匹配的 PFInstallation 的数据。您需要通过循环运行它,并将每个 objectAtIndex 设置为 PFInstallation。从那里开始,只需执行一个简单的 addUniqueObject,瞧,你就完成了。

在我的例子中,当登录时,它将 objectId 复制到一个名为 usersObjectId 的键,这是我为 PFInstallation 创建的。因此,我登录我的 iPhone,然后再次登录我的 iPad,我有 2 个不同的 PFInstallations,但都具有相同的 usersObjectId。运行所有这些代码允许我隔离所有我拥有的安装并编辑它们,特别是与我用于订阅 Facebook 好友的代码一起使用,这样我就可以在他们发布内容时收到通知。

关于ios - 查询 PFInstallation 并从云代码添加到 channel 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575420/

相关文章:

iOS 转换器应用程序,度量之间的连接

android - 是否可以同时跟踪 Android 和 iOS 应用程序的下载情况?

iphone - 如何使用 parse.com 从特定日期检索所有对象?

javascript - 理解 Parse 中的 JavaScript Promise

.net - Heroku 应用程序在 TLS v1.0/1.1 生命周期结束后无法使用 https

swift - 如何显示用户在提要上发布消息的时间?

java - Appium-如何使用Java从iOS设备读取日志

ios - 无法识别的选择器发送到实例 Swift 2.1

ios - 保存字符串以快速解析数组

ios - 将 3 种不同的信息加载到 3 种不同类型的细胞