Parse iOS SDK: how to set user "online" status?

Scenario = I have an application that allows users to register and view other users who are also “online”. In order to establish each online user status, I believe that I should set the code ...

[PFUser currentUser] setObject:[NSNumber numberWithBool:YES] forKey:@"isOnline"];
[[PFUser currentUser] saveInBackground];

at certain times in the application when the user uses the application. Maybe appDelegate, but I'm not sure.

(if this is not correct, please correct me)

Question = Where should this code be installed inside the application so that it always keeps track of when the user is "online" and when he is "disconnected"?

(specify method names)

+4
source share
1

- lastActive Date, , ( ).

, "" , , , lastActive , - , , 2 .

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);
    });
});

registerActivity Cloud Function 1,5 , , , .

, . , (, -).

+10

Source: https://habr.com/ru/post/1546904/


All Articles