【问题标题】:Call Parse Cloud Function for current user为当前用户调用 Parse Cloud 函数
【发布时间】:2014-08-08 02:18:27
【问题描述】:

我正在尝试在 Parse 中运行一些云函数,以及我在 Stackoverflow (Parse iOS SDK: Calling Cloud Functions From Xcode) 上找到的一个问题。以下是我想在我的应用程序中调用的函数:

var moment = require("moment");

Parse.Cloud.define("registerActivity", function(request, response) {
    var user = request.user;
    user.set("lastActive", new Date());
    user.save().then(function (user) {
        response.success();
    }, function (error) {
        console.log(error);
        response.error(error);
    });
});

Parse.Cloud.define("getOnlineUsers", function(request, response) {
    var userQuery = new Parse.Query(Parse.User);
    var activeSince = moment().subtract("minutes", 2).toDate();
    userQuery.greaterThan("lastActive", activeSince);
    userQuery.find().then(function (users) {
        response.success(users);
    }, function (error) {
        response.error(error);
    });
});

我想在我的一个视图控制器的 viewDidLoad 中调用 registerActivity 函数。这是我到目前为止所拥有的。我需要一些帮助来完成这个功能:

override func viewDidLoad() {
        super.viewDidLoad()

        let currentUser = PFUser.currentUser()

        PFCloud.callFunctionInBackground("registerActivity", withParameters: currentUser){
            if (!error){

            }
        }

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

我在“PFCloud”行收到一条错误消息:PFUser is not identical to [NSOBject:Anyobject]。如何为当前用户调用这个 registerActivity 函数?

谢谢!

更新

将 viewDidLoad 中的函数调用更新为:

PFCloud.callFunctionInBackground("registerActivty", withParameters: nil, block: nil)

这会导致异常:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

最后更新: 这样就解决了问题:

PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")

感谢您的帮助!

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    如果currentUser 已经登录,则无需提供。在服务器端request.user 将已设置为发出请求的登录用户。在这种情况下,您可以使用空字典的参数调用云函数。

    另一方面,您得到的错误是因为 PFUser 对象无法传递给云函数。您可以将用户 ID 传递为["id": currentUser.objectId]。然后在服务器中,您可以以request.params.id 访问它,以通过id 获取用户。这是为了以防您想为任何用户调用云功能(即使它没有登录)。

    【讨论】:

    • 感谢您的回复!我将函数调用更改为PFCloud.callFunctionInBackground("registerActivty", withParameters: nil, block: nil),但现在运行应用程序时出现异常。 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' 我得到这个是因为我需要传入 params.id 而不是 nil 吗?
    • 你需要传递一个空字典而不是 nil。在 Objective-C 中,它将是 @{}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多