如何从Firebase检索嵌套数据?应用程序崩溃

时间:2016-11-20 20:32:45

标签: ios swift firebase firebase-realtime-database firebase-storage

我的Firebase存储空间如下所示

我能够在“兴趣”下保存得很好,但是当我尝试检索用户ID下的所有数据时,应用程序崩溃了。我是Firebase的新手,我没有足够的经验知道如何调用所有数据(所有内容:电子邮件,位置,名称等以及感兴趣的孩子)。

有人可以帮帮我吗?这是我的代码:

 func fetchUser() {
    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        //for some reason uid = nil
        return
    }

    FIRDatabase.database().reference().child("users").child(uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {
            //                self.navigationItem.title = dictionary["name"] as? String

            let user = User()
            user.setValuesForKeysWithDictionary(dictionary)
            self.setUpProfileInformationWithUser(user)
        }

        }, withCancelBlock: nil)
}

...

 class User: NSObject {
     var id: String?
     var name: String?
     var email: String?
     var profileImageUrl: String?
     var username: String?
     var location: String?
     var userDescription: String?
     var interest : String?
     var interest_description : String?
 }

...

    2016-11-20 12:50:53.999 Your Niche[34689:1654014] -[__NSDictionaryI                 length]: unrecognized selector sent to instance 0x7f8bbd00f620
    2016-11-20 12:50:54.052 Your Niche[34689:1654014] *** Terminating         app due to uncaught exception 'NSInvalidArgumentException', reason: '-        [__NSDictionaryI length]: unrecognized selector sent to instance                 0x7f8bbd00f620'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x00000001055c9d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000107b5cdeb objc_exception_throw + 48
2   CoreFoundation                      0x00000001055d2d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x0000000105518cfa ___forwarding___ + 970
4   CoreFoundation                      0x00000001055188a8 _CF_forwarding_prep_0 + 120
5   libswiftCore.dylib                  0x000000010810c0c8 _TTSf4g_d___TFSSCfT12_cocoaStringPs9AnyObject__SS + 120
6   libswiftCore.dylib                  0x00000001080cc873 _TFSSCfT12_cocoaStringPs9AnyObject__SS + 19
7   libswiftFoundation.dylib            0x0000000108485d70 _TF10Foundation24_convertNSStringToStringFGSqCSo8NSString_SS + 16
8   Your Niche                          0x0000000103fb2f8b _TToFC10Your_Niche4Users8interestGSqSS_ + 75
9   Foundation                          0x000000010615219b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
10  Foundation                          0x000000010619399e -[NSObject(NSKeyValueCoding) setValuesForKeysWithDictionary:] + 261
11  Your Niche                          0x0000000103fbe790 _TFFC10Your_Niche21ProfileViewController9fetchUserFT_T_U_FCSo15FIRDataSnapshotT_ + 464
12  Your Niche                          0x0000000103f9fc1c _TTRXFo_oCSo15FIRDataSnapshot_dT__XFdCb_dS__dT__ + 60
13  Your Niche                          0x0000000104081431 __71-[FIRDatabaseQuery observeSingleEventOfType:withBlock:withCancelBlock:]_block_invoke + 37
14  Your Niche                          0x0000000104081731 __92-[FIRDatabaseQuery observeSingleEventOfType:andPreviousSiblingKeyWithBlock:withCancelBlock:]_block_invoke + 110
15  Your Niche                          0x00000001040a7dcf __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.64 + 88
16  libdispatch.dylib                   0x00000001085fbd9d _dispatch_call_block_and_release + 12
17  libdispatch.dylib                   0x000000010861c3eb _dispatch_client_callout + 8
18  libdispatch.dylib                   0x00000001086041ef _dispatch_main_queue_callback_4CF + 1738
19  CoreFoundation                      0x00000001055230f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
20  CoreFoundation                      0x00000001054e4b99 __CFRunLoopRun + 2073
21  CoreFoundation                      0x00000001054e40f8 CFRunLoopRunSpecific + 488
22  GraphicsServices                    0x0000000109403ad2 GSEventRunModal + 161
23  UIKit                               0x00000001065a8f09 UIApplicationMain + 171
24  Your Niche                          0x0000000103f857d2 main + 114
25  libdyld.dylib                       0x000000010865092d start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 

1 个答案:

答案 0 :(得分:0)

您的应用因user.setValuesForKeysWithDictionary(dictionary)而崩溃。在您的类定义中,interest只是另一个属性,但是当您从Firebase检索它时,它实际上是一个字典,因此编译器无法分别映射它。我能想到的两个选择真正的快速

  1. interest更改为字典而非您的班级定义
  2. 以旧学校方式提取数据,如下所示

    FIRDatabase.database().reference().child("users").child(uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
    
    if let dictionary = snapshot.value as? [String: AnyObject] {
        //                self.navigationItem.title = dictionary["name"] as? String
    
        guard let email = dictionary["email"] as! String, location = dictionary["location"] as! String, name = dictionary["name"] as! String, imageURL = dictionary["profileImageUrl"] as! String, description = dictionary["userDescription"] as! String, userName = dictionary["username"] as! String, interestDic = dictionary["interest"] as! [String : AnyObject] else
        {
            print("Error extracting FrB data")
            return
        }
    
        let interest = interestDic["interest"] as String
    
        let user = User(email, location, imageURL, description, userName, interest)
    
        self.setUpProfileInformationWithUser(user)
    }
    
  3. 免责声明:我个人没有尝试过第一个选项,但值得尝试。