1

I have created a login and signup page in my iPhone app linked to firebase-Auth. I also have a view controller where people can submit text. I am wanting to know if there is a way to start a 24 hour timer when the user has first submitted so when the timer reaches 0, the user is able to submit again.

Dravidian
  • 9,945
  • 3
  • 34
  • 74
Sam
  • 145
  • 1
  • 15

1 Answers1

2

What i am guessing is that you are using a button for submitting that text So every time your user clicks that button, send a call to your Firebase Database and check for the timestamp last posted , if that timestamp is older than 24 hours you let the user submit the text otherwise...

Saving the Text First time

FIRDatabase.database().reference().child(FIRAuth.auth()!.currentUser!.uid).child("lastPostedText").setValue(["text" : "This is the text", "timeStamp " : Int(NSDate.timeIntervalSinceReferenceDate*1000)])

Retrieving your Data at every button click

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 would look something like this:-

lastPostedText:{
   text : ...,
    timeStamp : 1234234556

   }

For finding the difference between your currentDate and timestamp see this answer:- https://stackoverflow.com/a/27184261/6297658

and converting timeStamp to NSDate, just :-

var date = NSDate(timeIntervalSinceReferenceDate: timestamp)

For knowing the time lapsed between the timeStamp and the current date timestamp look up : This answer

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Firstly thanks for your answer @Dravidian , Although i have some issues with it including- What is snapDict and how do i check if the user has posted 24 hours before! – Sam Oct 22 '16 at 07:14