Search code examples
swiftfirebasensdate

How store tomorrow NSDate into Firebase?


I'm working on an app where I store images into Firebase with a limit of time, I want to store a 24-hour limit on Firebase but is not working for me. Here is my function that handles the information that has to be saved on the Firebase Database.

     fileprivate func saveDataToUserProfile(imageUrl: String) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    guard let postImage = previewImageView.image else { return }
    let limitPostTime = Date(timeIntervalSinceNow: 60 * 60 * 24)


    let userRef = Database.database().reference().child("vibes").child(uid)
    let ref = userRef.childByAutoId()
    let values = ["vibeImageUrl": imageUrl, "imageWidth": postImage.size.width, "imageHeight": postImage.size.height, "creationDate": Date().timeIntervalSince1970, "votes": 0, "limitPostTime": limitPostTime] as [String : Any]
    ref.updateChildValues(values) { (err, ref) in
        if let err = err {
            print("Failed to save into DB: ", err)
            return
        }
            print("Successfully saved into the DB")
            self.removeFromSuperview()
    }
}

I try to use different ways to store but so far I haven't find the correct one.

let postLimitTime = Date().addingTimeInterval(60 * 60 * 24)

I want just to store tomorrows date, something like this now = 01/20/2019 tomorrow = 01/21/2019 of course in NSDate format.


Solution

  • you are trying to save a value of type NSDate into firebase as this answer says you need to store your value as a number

    var interval = NSDate().timeIntervalSince1970
    

    And when you need the date just get it like this

    var date = NSDate(timeIntervalSince1970: interval)