菜鸟教程小白 发表于 2022-12-11 18:55:20

iOS - Firebase 返回 null 但包含数据的字段存在


                                            <p><p>我有数据库“通知”。</p>

<pre><code>{
&#34;Notification&#34;: {
      &#34;user_id&#34;:
      &#34;date&#34;:
      &#34;type&#34;:
      &#34;info&#34;: {
         &#34;who_id&#34;:
         &#34;whom_id&#34;:
      }
    }
}
</code></pre>

<p>当我第一次启动应用程序时,我会从“通知”获取所有数据。当我将新数据添加到“通知”时,我获取的数据没有“信息”字段。只有字段:日期、类型、user_id。 </p>

<p>我的代码:</p>

<pre><code>func fetchNotitfications() {
      FIRDatabase.database().reference().child(&#34;Notification&#34;).queryOrdered(byChild: &#34;user_id&#34;).queryEqual(toValue: FIRAuth.auth()?.currentUser?.uid).observe(.childAdded, with: { (snapshot) in

            if let notificationDict = snapshot.value as? {
                let notification = Notification()
                notification.setValuesForKeys(notificationDict)

                self.notificationTableView.arrayNotification.append(notification)
                self.notificationTableView.reloadData()

            }   
      })
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您使用 childadded 时,返回的快照与您预期的不同。 </p>

<p>而不是得到`</p>

<pre><code>&#34;Notification&#34;: {
      &#34;user_id&#34;:
      &#34;date&#34;:
      &#34;type&#34;:
      &#34;info&#34;: {
         &#34;who_id&#34;:
         &#34;whom_id&#34;:
      }
    }
</code></pre>

<p>根据查询过滤数据的方式,您很可能会得到以下结果。</p>

<pre><code>[&#34;user_id&#34;:
&#34;date&#34;:
&#34;type&#34;:
&#34;info&#34;: {
       &#34;who_id&#34;:
       &#34;whom_id&#34;:
    }
]
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS - Firebase 返回 null 但包含数据的字段存在,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42167395/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42167395/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS - Firebase 返回 null 但包含数据的字段存在