Quickblox Online User IOS Status

I am using quickblox sdk for IOS and I have one request for online user status.

The code below is recommended.

QBUUser *user = ...;

NSInteger currentTimeInterval = [[NSDate date] timeIntervalSince1970];
NSInteger userLastRequestAtTimeInterval   = [[user lastRequestAt] timeIntervalSince1970];

// if user didn't do anything last 1 minute (60 seconds)    
if((currentTimeInterval - userLastRequestAtTimeInterval) > 60)
{ 
 // user is offline now
}

But my request is that I need to check this in the timer event, because every 5-10 seconds I would like to know if the user is online or is there any other better approach?

+4
source share
2 answers

Kudi, the only way to find out the last date of a user request is to use QBUUser lastRequestAt.

+1
source

Here is my user function to know the user presence. I set a timer that starts every 10 seconds.

 func update() {
            let currentTimeInterval: Int = Int(NSDate().timeIntervalSince1970)
            var userlastrew = Int()

            let rid = UInt(dialog.recipientID)
            var differ = Int()

            QBRequest.userWithID(rid, successBlock: { (response : QBResponse, user: QBUUser?) -> Void in
                userlastrew = Int((user?.lastRequestAt?.timeIntervalSince1970)!)
                differ = currentTimeInterval - userlastrew

                if differ > 120 {
                    let (d,h,m) = self.secondsToHoursMinutesSeconds(differ)
                    var txt = String()
                    if (d <= 0) {
                        if (m > 60 && h > 1 ) {
                            txt = "Last online \(h) hours ago"
                        }
                        if (m >= 60 && h == 1) {
                            txt =  "Last online \(h) hour ago"
                        }
                        if (m < 60 && h < 1) {
                            txt = "Last online \(m) minutes ago"
                        }
                        if (m < 60 && h == 1) {
                            txt = "Last online \(h) hour ago"
                        }
                        if (m < 60 && h > 1) {
                            txt = "Last online \(h) hours ago"
                        }
                    } else {
                        if (d > 1) {
                            txt = "Last online \(d) days ago"
                        } else {
                            txt = "Last online \(d) day ago"
                        }

                    }
                    //user is offline
                } else {
                    //user is online   
                }
                }, errorBlock: {(response: QBResponse) -> Void in
                    // Handle error
            })
        }

func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
        return (seconds / 86400, seconds / 3600, (seconds % 3600) / 60)
    }
0

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


All Articles