如何处理[NSDate]和Firebase [英] How to deal with [NSDate] and Firebase

查看:170
本文介绍了如何处理[NSDate]和Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个日历应用程序。我有一个选定日期的数组。用户选择它之前,它们被存储在那里。



var selectedDays = [NSDate]()



当应用程序加载时,我必须显示这些日期,但一切都过于复杂,因为Firebase不接受 NSDate



我有另外一个数组 var selectedDaysForFirebase = [String]()它与上面相同的数组只有日期被转换为字符串以适应Firebase。



这是我保存选定日期的方式:

  / / 1.将转换日期追加到selectedDaysForFirebase数组
let day = dateFormatter.stringFromDate(date)
selectedDaysForFirebase.append(day)
$ b $ // 2.将数据推送到Firebase
ref.childByAppendingPath(dates)。setValue(selectedDaysForFirebase)

我的Firebase具有用户身份验证功能。所以每个用户都有它自己的小日期数据库。



在我的 viewDidLoad 我必须抓住日期数组Firebase并将其存储在 selectedDates 数组中,它接受 NSDate



< pre $ ref.childByAppendingPath(dates)。observeEventType(.Value,withBlock:{snapshot in

如果让dates = snapshot.value as?NSArray {
print(dates)
//将firebase数组的日期与只接受NSDate
}

}的日期数组进行转换,withCancelBlock:{
中的错误print(error.description)
})

输出:$ b (
2016-04-10,
2016-04-11,
2016- 04-12

我认为有更好的方法来存储日期,然后检索它,我希望有人可以帮助我。

解决案

答案真的取决于你如何使用您的日期



数字不能被储存在火力地堡。任何数值首先被转换(通过Firebase)到NSNumber进行存储。请参阅了解数据



您需要确定如何使用日期以及需要多大的粒度。在某些情况下


2016-04-06

足够了,但是其他时间您可能需要更多


2016-04-06 14:00:00


$ / b

我建议简单地将它们存储为字符串,因为它们是可排序的,易于操作,并且可以被引入到NSDate对象中或从NSDate对象中取出。 / p>

以下是一些将NSDate转换为格式化字符串的示例代码,类似于时间戳记,然后可以将其作为字符串存储在Firebase中。 b

  NSDateFormatter * dateFormat = [NSDateFormatter new]; 
[dateFormat setDateFormat:@yyyy-MM-dd HH:mm:ss zzz];

NSString * dateString = [dateFormat stringFromDate:[NSDate date]];

NSLog(@%@,dateString);

以及从格式化字符串到NSDate的另一种方式

  NSDate * d = [NSDate new]; 

d = [dateFormat dateFromString:dateString];

NSLog(@date%@,d);

最重要的是:保持一致。但是你这样做的话,每次在你的代码中都要这样做。



哦 - 另外一件事。 Array的。数组应该谨慎使用,并且只能在Firebase的特定用例情况下使用。由于您无法访问特定的索引,或者有能力更改,添加或删除单个索引,因此可能会遇到挑战。



结构

  my_dates 
date_0
date_stamp:2016-04-06
date_1
date_stamp:2016-04-07

使用childByAutoId生成日期键(date_0,date_1)。

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.

这篇关于如何处理[NSDate]和Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆