简体   繁体   English

Swift iOS-即使存在有效数据,Firebase是否可能损坏数据并返回Nil值?

[英]Swift iOS -Is it Possible For Firebase to Corrupt Data and Return a Nil Value Even Though Valid Data Exists?

Before I send data to Firebase I make sure that a value definitely exists. 在将数据发送到Firebase之前,请确保确实存在一个值。 At several different points throughout my app I pull that data from Firebase Database. 在整个应用程序的几个不同点上,我从Firebase数据库中提取了该数据。 Is there any chance that a mistake can be made while Firebase is retrieving that data and it returns nil instead of the actual data? Firebase检索数据并返回nil而不是实际数据时,是否有可能出错?

Basically can Firebase corrupt valid data and return it in the form of a nil value? 基本上,Firebase可以破坏有效数据并以nil值的形式返回吗?

nameRef?.observeSingleEvent(of: .value, with: {
            (snapshot) in

            if let dict = snapshot.value as? [String:Any]{

                //100% these have valid data
                //but if either of these get corrupted and returned as nil there will be a crash
                let firstName = dict["firstName"] as? String
                let lastName = dict["lastName"] as? String

                self.firstNameLabel.text! = firstName!
                self.lastNameLabel.text! = lastName!            
            }
}

I know I can use if let to prevent a crash but I want to know is it even necessary? 我知道可以使用if let来防止崩溃,但是我想知道是否有必要? I have way more data being pulled then firstName and lastName and I would have to use if let all over the place. 我有更多的数据被提取,然后是firstName和lastName, if let放到所有地方,我将不得不使用。 Even though it's the safe thing to do it's adds a lot of code throughout my app. 即使这样做是安全的,它仍在我的应用程序中添加了很多代码。 If unnecessary I won't use it. 如果不必要,我不会使用它。

if let firstName = firstName{....
if let lastName = lastName{....

This is my first time building an app, and I've never worked on or with a team before so I don't know the answer to a question like this. 这是我第一次构建应用程序,而我之前从未与任何团队合作过,所以我不知道这样的问题的答案。

No, when you retrieve values from Firebase you are sure you get the values from the database correctly, when the values are not in your cache . 不,当您从Firebase检索值时,请确保当值不在您的缓存中时,可以正确地从数据库中获取值。 A common mistake I see here is that they have persistence on in there appDelegate, and they do not sync the database before capturing a singleEvent, like you are doing. 我在这里看到的一个常见错误是,它们在appDelegate中具有持久性,并且在捕获singleEvent之前不像您所做的那样同步数据库。 When using childAdded, childRemoved etc, the database is always in sync, so you do not have to call keepSynched/turn persistence off. 使用childAdded,childRemoved等时,数据库始终处于同步状态,因此您不必调用keepSynched /关闭持久性。

Below code looks cleaner, safer and more understandable. 下面的代码看起来更干净,更安全且更易于理解。 I just added default values. 我刚刚添加了默认值。

nameRef?.observeSingleEvent(of: .value, with: {
            (snapshot) in
                let values = snapshot.value as? NSDictionary
                self.firstNameLabel.text! = dict["firstName"] as? String ?? "No first name given"
                self.lastNameLabel.text! = dict["lastName"] as? String ?? "No last name given"
}

If you have persistence on in your appDelegate, call nameRef?.keepSynced(true) before capturing data with a observeSingleEvent. 如果您在appDelegate中具有持久性,请在使用observeSingleEvent捕获数据之前调用nameRef?.keepSynced(true)。 Not calling this can result in strange snapshot values, like this one: How to retrieve data from firebase using Swift? 不调用它会导致奇怪的快照值,如下所示: 如何使用Swift从Firebase检索数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS-Firebase返回null,但存在包含数据的字段 - iOS - Firebase return null but field with data exists Swift iOS Firebase-如果FirebaseAuth.FIRUser对象不是nil,则其上的.email如何返回nil? - Swift iOS Firebase -If the FirebaseAuth.FIRUser Object isn't nil how is it possible for the .email on it to return nil? json数据在swift中返回nil - json data return nil in swift iOS Receipt Data 在其他设备中返回 nil - iOS Receipt Data return nil in other devices 查询Firebase嵌套数据以检查特定值-iOS Swift - Query Firebase nested data to check specific value - iOS Swift 从 Firebase 实时数据库读取数据并按值排序 - iOS - Swift - Reading data from a Firebase Realtime Database and sorting it by value - iOS - Swift Realm Swift:即使没有数据更改,结果也会收到通知(Swift) - Realm Swift: Results getting notifications even though there is no data change (Swift) Swift iOS -DispatchWorkItem仍在运行,即使它已取消并设置为Nil - Swift iOS -DispatchWorkItem is still running even though it's getting Cancelled and Set to Nil 是否可以通过AB API破坏iOS AddressBook中的数据? - Is it possible to corrupt somehow data in iOS AddressBook via AB API? (iOS Swift)从Core Data获取记录返回致命错误或为nil - (iOS Swift) Fetching record from Core Data return fatal error or nil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM