【发布时间】:2022-01-22 15:10:21
【问题描述】:
我意识到您无法在 Firestore 中查询“不包含”数组,这是非常有问题的。 (我无法查询用户没有看到的内容,但是我想知道您是否可以比较集合并因此突出相似之处)
有没有办法对 Firestore 进行 2 次查询,然后比较集合中是否存在类似字段?
例如,如果我可以进行 2 次查询以将集合“AllNews”与集合“MyNews”进行比较,从而能够在 collectionviewcell 中突出显示“uid1”和“uid2”,因为这两个集合中都存在这些项目。这样,用户可以查看“AllNews”中的所有项目,同时突出显示他的收藏“MyNews”中已有的项目。
AllNews
uid1 *
uid2 *
uid3
uid4
uid5
MyNews
uid1 *
uid2 *
正在获取“AllNews”集合。
我还需要以某种方式获取“MyNews”集合...
func fetchAllNews(firebasePath: Query) {
let allnewsref = firebasePath
allnewsref.getDocuments() { (querySnapshot, err) in
if err == nil && querySnapshot != nil {
var tempNews = [CreatedAllNews]()
if querySnapshot!.documents.count > 0 {
self.dataExists()
for document in querySnapshot!.documents {
let data = document.data()
let createdAllNews = CreatedAllNews(newsId: document.documentID)
tempNews.append(createdAllNews)
}
}
self.news = tempNews
}
}
}
如果两个数组中都存在字段,则以某种方式比较数组并突出显示单元格。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllNewsCollectionViewCell
cell.set(createdAllNews: allNews[indexPath.row])
//If item exists within array "createdAllNews" and array "MyNews"
cell.layer.borderColor = UIColor.blue
}
return cell
}
即使有一个大型数据库,我也希望它能够有效地工作。这是可能的还是有其他方法可以达到类似的最终结果?感谢您对解决方案的任何推动。
【问题讨论】:
-
我想我有一个解决方案,但需要澄清问题;文本状态 - 突出显示他已经在他的集合“MyNews”中拥有的那些 - 但是,显示的结构是 MyNews 不是每个用户的集合 - 它显示用户为带有 MyNews 集合的文档(与文本所述相反)。这种关系对于解决方案很重要。让我们重新陈述问题;您的目标是让每个用户都能看到所有新闻,并在 UI 中突出显示所有新闻中的某些新闻文章,以便用户知道他们正在关注哪些新闻文章?
标签: ios swift firebase google-cloud-platform google-cloud-firestore