0

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.

Robby
  • 197
  • 1
  • 2
  • 14

1 Answers1

1

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)
AJ_iOS
  • 66
  • 5
  • To convert Tomorrow date var interval = (NSDate().addingTimeInterval(60*60*24)).timeIntervalSince1970 – AJ_iOS Jan 21 '19 at 10:03
  • Thank you, I know I was little bit close but I didn't even know that Firebase will not take NSDate so we have to play around with timeInterval. – Robby Jan 21 '19 at 18:47