【问题标题】:Firebase and timer's, Changing permissions based on time passedFirebase 和计时器,根据经过的时间更改权限
【发布时间】:2016-10-22 04:15:20
【问题描述】:

我在我的 iPhone 应用程序中创建了一个与 firebase-Auth 相关联的登录和注册页面。我还有一个视图控制器,人们可以在其中提交文本。我想知道是否有办法在用户首次提交时启动 24 小时计时器,以便当计时器达到 0 时,用户可以再次提交。

【问题讨论】:

    标签: firebase swift2 xcode7 nstimer firebase-authentication


    【解决方案1】:

    我猜您正在使用一个按钮来提交该文本因此,每次您的用户单击该按钮时,请向您的 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

    【讨论】:

    • 首先感谢您的回答@Dravidian,虽然我有一些问题,包括-什么是 snapDict 以及如何检查用户是否在 24 小时前发布!
    猜你喜欢
    • 2018-04-18
    • 2023-04-02
    • 1970-01-01
    • 2021-09-04
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多