1

i was wondering how you automatically delete a Firebase Child if the child live longer than 24 hours. My code to save the data is:

@IBAction func Sturen(_ sender: Any) {
        let Extrainfo:String = self.Extrainformatie.text!
        let date = Date()
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: date)
        let minutes = calendar.component(.minute, from: date)
        let tijd = "\(hour) : \(minutes)"

        let random = Int(arc4random_uniform(99999))
        var randomstring = String(random)

        self.ref.child("Rollerbanken").child(randomstring).setValue(["Extra Informatie": Extrainfo, "TypeControle": TypeC, "Tijd": tijd, "Stad": Stad, "Latitude": lat, "Longitude": long])

        self.performSegue(withIdentifier: "Show map", sender: nil)

    }

Does anybody knows how to do this? Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dani Kemper
  • 343
  • 2
  • 10
  • surely you would do that with a firebase "function" on the firebase cloud? it's a few lines of javascript – Fattie Sep 14 '17 at 23:59
  • Yeah i saw that already but the point is, i don't know how to do it.. So can you explain it for me? – Dani Kemper Sep 15 '17 at 00:03
  • While Cloud Functions may be a natural environment to run periodic code, it's also possible to do this in Swift code. As long as the code runs periodically in the device, it can delete older items from the database. What specific part are you having problems with? If you're looking for inspiration for an approach, in [my answer to this question](https://stackoverflow.com/questions/32004582/delete-firebase-data-older-than-2-hours) I've shown how to do this in JavaScript (both client-side and in Cloud Functions). – Frank van Puffelen Sep 15 '17 at 03:20
  • 1
    hi Frank, absolutely do not do that. it has to be done in cloud code. – Fattie Sep 15 '17 at 13:20
  • hi Dani - Frank pointed you to a magnificent answer of his where you get copy and paste cloud code. Enjoy – Fattie Sep 15 '17 at 13:20
  • there was an excellent answer below which was deleted by a very silly deletion. As it said, Check out the docs https://firebase.google.com/docs/functions/get-started and follow them. Its very easy, like breathing – Fattie Sep 15 '17 at 13:21
  • 1
    Possible duplicate of [Delete firebase data older than 2 hours](https://stackoverflow.com/questions/32004582/delete-firebase-data-older-than-2-hours) – Fattie Sep 16 '17 at 03:08

0 Answers0