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.
The answer really depends on how you are using your date.
'numbers' cannot be stored in Firebase; any numeric value is first converted (by Firebase) to NSNumber for storage. See Understanding Data
You need to determine how you will be using your date and how much granularity will be needed. In some cases
2016-04-06
is enough, but other times you may need more
2016-04-06 14:00:00
I would suggest simply storing these as strings as they are sortable, easily manipulated and can be brought into a NSDate object or taken out of an NSDate object.
Here's some sample code to convert NSDate to a formatted string, similar to a time stamp, which can then be stored in Firebase as a string.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(@"%@",dateString);
and the other way from formatted string to NSDate
NSDate *d = [NSDate new];
d = [dateFormat dateFromString:dateString];
NSLog(@"the date %@", d);
The super most important thing is: keep it consistent. However you do it, do it the same way every time in your code.
Oh - one other thing. Array's. Array's should be used sparingly and only in specific use case situations in Firebase. They can be challenging to work with as you don't have access to specific indexes or have the ability to change, add or remove a single index.
Here's a much better structure
my_dates
date_0
date_stamp: 2016-04-06
date_1
date_stamp: 2016-04-07
the date keys (date_0, date_1) are generated with childByAutoId.
get a date from a double:
var interval = Double()
var date = NSDate()
date = NSDate(timeIntervalSince1970: interval)
other way around, get double from date:
interval = date.timeIntervalSince1970
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With