I am making a calendar app. I have one array of selected dates. User selected it previously and they are stored there.
var selectedDays = [NSDate]()
When app loads I have to display these dates, but everything is overcomplicated because Firebase doesn't accept NSDate
I have another array var selectedDaysForFirebase = [String]()
which is the same array as above only dates are converted to strings to accomodate Firebase.
This is how I save selected dates:
// 1. Append converted date into selectedDaysForFirebase array
let day = dateFormatter.stringFromDate(date)
selectedDaysForFirebase.append(day)
// 2. Push data to Firebase
ref.childByAppendingPath("dates").setValue(selectedDaysForFirebase)
My Firebase has user auth. so every user has it's own little database with dates.
In my viewDidLoad
I have to grab that dates array from Firebase and store it in my selectedDates
array which accepts NSDate
ref.childByAppendingPath("dates").observeEventType(.Value, withBlock: { snapshot in
if let dates = snapshot.value as? NSArray {
print(dates)
// convert firebase array of dates with array of dates that only accepts NSDate
}
}, withCancelBlock: { error in
print(error.description)
})
This outputs:
(
"2016-04-10",
"2016-04-11",
"2016-04-12"
)
I think there is a better way to store dates and then retreive it and I hope somebody could help me with this.