简体   繁体   English

使用云代码处理parse.com类

[英]manipulating parse.com class with cloud code

I have a random messaging app using parse.com as a backend. 我有一个使用parse.com作为后端的随机消息传递应用程序。 I am saving the messages in a class called "Messages" with the keys: 我使用以下键将消息保存在名为“消息”的类中:

  • "messageBody"(which is the content of the message) and “ messageBody”(这是消息的内容)和
  • "senderId"(which is the sender id of course) “ senderId”(当然是发送者ID)

After this gets saved in my Messages class, I am using cloud code to query 3 random users and send this message to them. 将其保存在我的Messages类中之后,我正在使用云代码查询3个随机用户并将此消息发送给他们。

My question is which is the best way to do this because I foresee errors in my current method. 我的问题是执行此操作的最佳方法,因为我预见了当前方法中的错误。 The current method i am using is after the user presses send I save the message to Parse.com and then I call the "send to 3 random users" cloud function, but what if my message has not been successfully saved to the parse backend before the cloud function is implemented? 我正在使用的当前方法是在用户按下发送后,将消息保存到Parse.com,然后调用“发送给3个随机用户”云函数,但是如果我的消息尚未成功保存到解析后端,该怎么办?云功能实现了吗?

  -(IBAction)send{

      PFObject *message = [PFObject objectWithClassName:@"Message"];
      [message setObject:self.messageContent forKey:@"messageBody"];
      [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];

      [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        if(error){
           //show alert with error
        }
        else{
           //everything was successful
        }
       }];



       [PFCloud callFunctionInBackground:@"sendToThreeRandomUsers" withParameters:@{} 
                                                                   block:^(NSString *result, NSError *error) {
                                                                       if (!error) {
                                                                          //cloud function was a success!
                                                                       }
                                                                    }];

  }

Basically I want to know if there is a way that whenever there is a new object in the Messages class I can say send this to 3 random users from my parse backend rather than calling it from my users device? 基本上,我想知道是否有一种方法,只要Messages类中有一个新对象,我可以说可以将其从解析后端发送给3个随机用户,而不是从用户设备中调用?

Or Should I just totally skip saving it to my parse backend and just send it straight to my cloud code as a parameter of the cloud function? 还是我应该完全跳过将其保存到解析后端的过程,而直接将其作为云函数的参数发送到我的云代码中? Then save it to my backend. 然后将其保存到我的后端。 what if the messageBody is very big though? 如果messageBody非常大怎么办?

So this question really isnt about code but the way to structure it. 因此,这个问题实际上与代码无关,而是结构代码的方式。

Wish I could use Hector Ramos as a tag for this question 希望我可以使用Hector Ramos作为此问题的标签

Why don't you write a afterSave method for your Messages class. 为什么不为Messages类编写afterSave方法。 Whenever new message is saved successfully, this method (Parse.Cloud.afterSave("Messages", function(request, response) {..}) is executed and 3 random users can be selected. The API explanation is in below link; 每当成功保存新消息时,都会执行此方法(Parse.Cloud.afterSave(“ Messages”,function(request,response){..}),并可以选择3个随机用户。

https://parse.com/docs/cloud_code_guide#functions-onsave https://parse.com/docs/cloud_code_guide#functions-onsave

Hope this helps, Regards. 希望这会有所帮助,问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM