我一直在使用IOS版的quickblox sdk,我有一个关于用户在线状态的问题。
下面是推荐的代码。
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
}但我的疑问是,我是否需要在计时器事件中检查这一点,因为每隔5-10秒,我就想知道用户是否在线或是否有其他更好的方法?
发布于 2015-09-04 16:18:19
Kudi,找出用户请求的最后日期的唯一方法是使用QBUUser lastRequestAt。
发布于 2016-06-10 22:58:27
下面是我的自定义函数,用于了解用户的状态。我设置了一个计时器,每10秒计时一次。
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)
}https://stackoverflow.com/questions/32367296
复制相似问题