0

I have realized that you cannot query for a "does-not-contain" array in Firestore which is very problematic. (I cannot query for that which a user has not seen, I am however wondering if you can compare collections and therefore highlight similarities)

Is there a way to make 2 queries to Firestore and afterward compare if similar fields exist within the collections?

For example, if I could make 2 queries to compare the collection "AllNews" with the collection "MyNews" and therefore be able to highlight "uid1" & "uid2" within a collectionviewcell since the items exist within both collections. This way a user can see all items within "AllNews" while at the same highlighting the ones he already has in his collection "MyNews".

AllNews
     uid1 *
     uid2 *
     uid3
     uid4
     uid5

MyNews
     uid1 *
     uid2 *

Fetching the "AllNews" collection.

I would also need to fetch the "MyNews" collection somehow...


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
            }
        }
    }

Somehow comparing arrays and highlighting the cell if a field exists within both arrays.

 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
}

Optimally I would like this to work efficiently even though there is a large database. Is this possible or is there another way to achieve a similar end result? Any nudge towards a solution is appreciated.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Redstain
  • 157
  • 2
  • 10
  • I think I have a solution but need some clarity in the question; the text states - *highlighting the ones he already has in his collection "MyNews"* - however, the structure shown is that MyNews is not a collection of each user - it shows users as documents with the MyNews Collection (the opposite of what the text states). That relationship is important to a solution. Let re-state the question; your goal is for each user to be able to see All News and have certain news articles within All News be highlighted in the UI so that the user knows which news articles they are following? – Jay Jan 23 '22 at 17:49

1 Answers1

1

It will be very costly to compare the documents in a collection against the documents in another collection. Why? Because you need to read all the documents within both collections. It will be fine if your collections only hold a few documents but as the number of documents grows, this operation isn't feasible.

So to solve this, you should use an alternative solution, which will be to store only the UIDs in a document. You can either store them in an array or in a map. Then you only have to perform a single document read and compare the UIDs.

There is however a limitation, which is the maximum 1 MiB size of the document. If the arrays/maps for AllNews and MyNews don't fit into a single document then you should consider creating two documents, one for each category. If this isn't enough then you can create two or three documents for each category. In this way, you can reduce the number of reads consistently.

To make the comparison, please check the answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193