【发布时间】:2016-10-22 04:15:20
【问题描述】:
我在我的 iPhone 应用程序中创建了一个与 firebase-Auth 相关联的登录和注册页面。我还有一个视图控制器,人们可以在其中提交文本。我想知道是否有办法在用户首次提交时启动 24 小时计时器,以便当计时器达到 0 时,用户可以再次提交。
【问题讨论】:
标签: firebase swift2 xcode7 nstimer firebase-authentication
我在我的 iPhone 应用程序中创建了一个与 firebase-Auth 相关联的登录和注册页面。我还有一个视图控制器,人们可以在其中提交文本。我想知道是否有办法在用户首次提交时启动 24 小时计时器,以便当计时器达到 0 时,用户可以再次提交。
【问题讨论】:
标签: firebase swift2 xcode7 nstimer firebase-authentication
我猜您正在使用一个按钮来提交该文本因此,每次您的用户单击该按钮时,请向您的 Firebase 数据库发送调用并检查上次发布的时间戳,如果该时间戳早于 24 小时你让用户提交 文本 否则......
第一次保存文本
FIRDatabase.database().reference().child(FIRAuth.auth()!.currentUser!.uid).child("lastPostedText").setValue(["text" : "This is the text", "timeStamp " : Int(NSDate.timeIntervalSinceReferenceDate*1000)])
在每次点击按钮时检索您的数据
FIRDatabase.database().reference().child(FIRAuth.auth()!.currentUser!.uid).child("lastPostedText").observeSingleEventOfType(.Value, withBlock : {(snapshot) in
if snapshot.exists(){
if snapDict = snapshot.value as? [String:AnyObject]{
let timeSince = snapDict["timeStamp"] as! Double
// Check the timeSince against NSDate, if it has been more than 24 hours then let him post the text else show him an alert t
}
}else{
//User hasn't submitted even a single text yet
FIRDatabase.database().reference().child(FIRAuth.auth()!.currentUser!.uid).child("lastPostedText").setValue(["text" : "This is the text", "timeStamp " : NSDate.timeIntervalSinceReferenceDate])
}
)}
snapDict 看起来像这样:-
lastPostedText:{
text : ...,
timeStamp : 1234234556
}
要查找您的 currentDate 和时间戳之间的差异,请参阅此答案:- https://stackoverflow.com/a/27184261/6297658
并将时间戳转换为 NSDate,只需:-
var date = NSDate(timeIntervalSinceReferenceDate: timestamp)
要了解时间戳和当前日期时间戳之间经过的时间,请查找:This answer
【讨论】: