ios - 检查 Firestore 查询 (whereField isEqualTo) 是否未找到任何文档

标签 ios swift firebase google-cloud-firestore

我想检查我的 Firestore 查询是否找到了具有我想要的特定字段的任何文档。如果没有,我想继续编写其他代码。

不幸的是,我自己还没有找到解决这个问题的方法。你能帮忙吗?

代码:

Firestore.firestore().collection("conversations").whereField("mainUserID", isEqualTo: MainUID)
.whereField("otherUserID", isEqualTo: otherUserId).getDocuments { (snapshot, err) in
                
if snapshot.exists == true {                // Value of type 'QuerySnapshot?' has no member 'exists'
                        
                        
} else {
       
       }
}

最佳答案

来自文档

A FIRQueryDocumentSnapshot contains data read from a document in your Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted with the data property or by using subscript syntax to access a specific field.

A FIRQueryDocumentSnapshot offers the same API surface as a FIRDocumentSnapshot. As deleted documents are not returned from queries, its exists property will always be true and data: will never return nil.

重要的是这个

The document is guaranteed to exist

因此,由于保证快照的存在,.exists 选项没有意义。

一种方法是检查快照中有多少文档

if docs.count > 0 {
   //there are docs
} else {
   //there are no docs
}

关于ios - 检查 Firestore 查询 (whereField isEqualTo) 是否未找到任何文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63126931/

相关文章:

iphone - 安装新旧版本的 Xcode

ios - 如何快速从google place api获取地点图片

ios - 将新字符串添加到数组时如何在 UICollectionView 中创建新单元格

javascript - 使用 where 子句 firestore 搜索时获取文档 id

android - Firestore - 如何在数据库中自动递减整数值?

iphone - 解除分配当前/上一个 View Controller

ios - 什么时候使用 NSURLSessionDownloadTask 和 NSURLSessionDataTask?

swift - Cocoa 窗口在 macOS Sierra 上不显示

reactjs - 我的 React 应用程序的 Firebase 功能无法正常工作

ios - 使用 UISearchbar 和过滤后自定义新表