Awesome.Apple
Awesome.Apple

Reputation: 1324

Quickblox User online status IOS

I have been using quickblox sdk for IOS and i have one query regarding user's online status.

Below is the code which 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 query is that do i need to check this in a timer event because every 5-10 seconds i would want to know if user is online or there is any other better approach?

Upvotes: 4

Views: 721

Answers (2)

Utku Dalmaz
Utku Dalmaz

Reputation: 10172

Here is my custom function to know user's presence. I set a timer which runs 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)
    }

Upvotes: 0

Sabina Bashuk
Sabina Bashuk

Reputation: 189

Kudi, the only way to find out the last date of user's request is by using QBUUser lastRequestAt.

Upvotes: 1

Related Questions