如何从 Firebase 检索嵌套数据?应用程序崩溃
Posted
技术标签:
【中文标题】如何从 Firebase 检索嵌套数据?应用程序崩溃【英文标题】:How to retrieve nested data from Firebase?? App is crashing 【发布时间】:2016-11-20 20:32:44 【问题描述】:我的 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】:您的应用因user.setValuesForKeysWithDictionary(dictionary)
而崩溃。在您的类定义中,interest
只是另一个属性,但是当您从 Firebase 检索它时,它实际上是一个字典,因此编译器无法分别映射它。我能很快想到的两个选项
-
在您的类定义中将
interest
更改为字典
如下图所示的老派方式提取数据
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)
免责声明:我个人没有尝试过第一个选项,但值得一试。
【讨论】:
感谢回复,这两个选项我都试了好几次了,都说用户不能接受参数,所以不知道怎么解决。 你能发布你得到的确切错误吗?这可能是因为您没有构造函数,因此需要为每个新创建的对象分配所有属性 当我输入代码时出现了很多错误,但主要错误是: 条件绑定的初始化程序必须具有可选类型,而不是 '[String : AnyObject]' 然后当我删除 'guard' ,它带来了更多的错误。 你可能想看看这个我有一段时间关于检索 Firebase 数据的答案。stackoverflow.com/a/39862631以上是关于如何从 Firebase 检索嵌套数据?应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
关于在Firebase auth Uid下检索嵌套数据并使用android studio推送uid