Parse Cloud Code 中的调用函数
Posted
技术标签:
【中文标题】Parse Cloud Code 中的调用函数【英文标题】:Calling function in Parse Cloud Code 【发布时间】:2016-04-14 01:32:04 【问题描述】:我是第一次使用云代码并尝试调用以下函数:
let friendRequest: PFObject = self.friendRequestsToCurrentUser[sender.tag] as PFObject
let fromUser: PFUser = friendRequest[FriendRequestKeyFrom] as PFUser
//call the cloud code function that adds the current user to the user who sent the request and pass in the friendRequest id as a parameter
PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: ["friendRequest": friendRequest.objectId]) (object:AnyObject!, error: NSError!) -> Void in
let friendsRelation: PFRelation = PFUser.currentUser()!.relationForKey("friends")
friendsRelation.addObject(fromUser)
self.currentUser.saveInBackgroundWithBlock( (succeeded: Bool, error: NSError!) -> Void in
if succeeded
else
)
实现该功能后,我需要添加“!”到参数中的 objectId 以展开它。 但是,这样做会给我留下错误:
无法将 '(AnyObject!, NSError!) -> Void' 类型的值转换为 预期参数类型“PFIdResultsBlock?”
我必须改变什么才能调用这个函数?
【问题讨论】:
【参考方案1】:PFIdResultsBlock
对应于以下签名(AnyObject?, NSError?) -> Void
,因此请尝试将您的代码更改为:
let friendRequest: PFObject = self.friendRequestsToCurrentUser[sender.tag] as PFObject
let fromUser: PFUser = friendRequest[FriendRequestKeyFrom] as PFUser
//call the cloud code function that adds the current user to the user who sent the request and pass in the friendRequest id as a parameter
PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: ["friendRequest": friendRequest.objectId]) (object:AnyObject?, error: NSError?) -> Void in
let friendsRelation: PFRelation = PFUser.currentUser()!.relationForKey("friends")
friendsRelation.addObject(fromUser)
self.currentUser.saveInBackgroundWithBlock( (succeeded: Bool, error: NSError!) -> Void in
if succeeded
else
)
【讨论】:
感谢您的帮助!【参考方案2】:尝试使用PFObject?
而不是AnyObject!
。
【讨论】:
我尝试了 PFObject 但仍然收到错误消息。谢谢你的建议。以上是关于Parse Cloud Code 中的调用函数的主要内容,如果未能解决你的问题,请参考以下文章
response.error不是Parse Cloud Code中的函数
Parse Cloud Code 嵌套查询上的代码 141(错误:未调用成功/错误)