【问题标题】:Query PFInstallation and add to channels key from cloud code从云代码中查询 PFInstallation 并添加到通道密钥
【发布时间】:2015-02-18 13:30:14
【问题描述】:

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

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。如何在云代码中做同样的事情?

【问题讨论】:

  • 您需要帮助将整个块转换为 Cloud Code 还是其中有错误?
  • 错误告诉我,我不能直接查询 PFInstallation,所以我知道它必须在云代码上完成,只是不确定如何。到目前为止,我有:Parse.Cloud.define("subscribingAll", function(request, response) { var usersObjectId = request.params.usersObjectId;   var newSubscription = request.params.newSubscription; var pushQuery = new Parse.Query(Parse.Installation); pushQuery.equalTo("usersObjectId", usersObjectId); });我只是不确定如何从这里开始使用每个 PFInstallations 中的查询和更新键
  • 嗯,不知道我能帮你多少。你有没有看过docs看看你是否能找到你需要的东西?
  • @lightice11 我看过它们,但看不到在 javascript 中我可以在哪里获取返回的安装文件并进行编辑。
  • 来自解析文档:Note that Installation data can only be modified by the client SDKs, the data browser, or the REST API.

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


【解决方案1】:

好的,经过几个小时的工作,我终于想通了,并认为会发布解决方案。如果您想编辑某种类型的所有 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 复制到我为 PFInstallation 创建的名为 usersObjectId 的键中。所以,我在我的 iPhone 上登录,然后在我的 iPad 上再次登录,我有 2 个不同的 PFInstallations,但都具有相同的 usersObjectId。运行所有这些代码可以让我隔离我拥有的所有安装并对其进行编辑,特别是与我用于订阅 Facebook 朋友的代码一起使用,以便在他们发布内容时通知我。

【讨论】:

    【解决方案2】:

    看起来您只能在当前设备上修改 PFInstallation,而不是全部来自一个用户或云端。

    这是解析时显示的代码:

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation addUniqueObject:@"Giants" forKey:@"channels"];
    [currentInstallation saveInBackground];
    

    也许一种解决方法是添加一个新的默认频道,您订阅每个用户都会向他们发送通知,如果他们应该订阅新频道?我不确定这是否是最佳解决方案,因为我不知道您在什么情况下执行此订阅。

    【讨论】:

    • 这个想法是这样的。您有一个表格视图,其中显示了所有使用该应用程序的 Facebook 朋友。单击一个获取他们的 objectId 以添加到频道。但是,我希望它订阅用户拥有的所有设备,因此我需要搜索当前用户的所有安装并更新每个设备上的频道以完成此操作。
    • 你想通过点击他们的名字来执行什么操作?
    • 点击他们的名字,将存储在该数组路径中的 Facebook id 与其 PFUser 上的键 fbId 匹配。检索 PFUser objectId,将其添加到与当前 PFUser 关联的每个 PFInstallation 上的通道中。
    • 我很确定当前用户只会有一个 PFInstallation 。只需使用[PFInstallation currentInstallation] 访问+更改它。
    • 会有多个。在 iPhone 上使用 Facebook 登录,有 1 个 PFInstallation。在 iPad 上使用同一个 Facebook 登录,还有另一个。
    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2011-01-09
    • 2020-03-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多