2017-09-05 24 views
0

我正根据我的文档配置我的数据库。我做了所有的初始配置:我会把下面当我尝试在Firebase数据库中插入Date作为关键字时发生崩溃

的AppDelegate代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     FirebaseApp.configure() 

     GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID 
     GIDSignIn.sharedInstance().delegate = self 
     FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 

     if (Auth.auth().currentUser) != nil { 
      presentHome() 
     } else { 
      presentSignIn() 
     } 

     return true 
    } 

的问题是,当我创建数据库参考,并具有插入东西,我得到这个错误:

terminating with uncaught exception of type NSException

的ViewController:

var ref: DatabaseReference! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     ref = Database.database().reference() 
    } 

@IBAction func saveTask(_ sender: Any) { 
     let task:[String:Any] = [ 
      "title":self.titleTxtField.text!, 
      "type":type!, 
      "observations":"Teste de tarefa", 
      "finished":false, 
      "images":[ 
       "image1":"https://static.pexels.com/photos/248797/pexels-photo-248797.jpeg", 
       "image2":"http://www.planwallpaper.com/images#static/images/beautiful-sunset-images-196063.jpg" 
       ] 
     ] 

     let date = Date().timeIntervalSince1970 
     ref.child("\(self.user!.uid)/tasks/\(date)").setValue(["teste":"teste"]) 
    } 

回答

1

我觉得你的问题是在这条线:

let date = Date().timeIntervalSince1970 

该打印一些字符,这是不允许进入作为火力地堡

这样子键,如果你想这样的结构:

root 
--- year 
------ month 
--------- day 

尝试这样做:

  1. 从日期提取DateComponents

  2. 的结果

创建一个新的字符串,并使用此代码添加新的字符串作为孩子:

let components = Calendar.current.dateComponents([.day, .month, .year], from: yourDate) 
let ref = Database.database().reference() 
ref.child("root/\(components.year!)/\(components.month!)/\(components.day!)") 
+0

这不是我的问题。如果我删除日期并传递一个简单的字符串,我有相同的错误 –

+0

请提供一些你的错误代码的屏幕,我会调查 –

+0

你是对的。但我也有配置错误。击中两个我能够记录我的数据! –

相关问题