Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在FIRDocumentChangeType中区分已删除或已删除_Ios_Firebase_Google Cloud Firestore - Fatal编程技术网

Ios 在FIRDocumentChangeType中区分已删除或已删除

Ios 在FIRDocumentChangeType中区分已删除或已删除,ios,firebase,google-cloud-firestore,Ios,Firebase,Google Cloud Firestore,我的主要目标是将firestore中的分页和实时性结合起来。 有没有办法在Snapshotlistener中知道某个对象是否已被删除,因为它不再与查询匹配,或者该对象是否已被删除?FIRDocumentChangeTypeRemoved的文档上写着“(要么已删除,要么已与查询不匹配)”。有没有办法知道其中哪一个实际发生了 我的方法如下: override func viewDidLoad() { super.viewDidLoad() getFirstBatchOfIt

我的主要目标是将firestore中的分页和实时性结合起来。
有没有办法在Snapshotlistener中知道某个对象是否已被删除,因为它不再与查询匹配,或者该对象是否已被删除?FIRDocumentChangeTypeRemoved的文档上写着“(要么已删除,要么已与查询不匹配)”。有没有办法知道其中哪一个实际发生了

我的方法如下:

override func viewDidLoad() {
    super.viewDidLoad()
    
    getFirstBatchOfItems()
}

private func getFirstBatchOfItems() {
 //getting initial 20 items and storing them in an array 'items'


self.startListeningForUpdates()
}

private func startListeningForUpdates() {        
    questionItemsListener = Firestore.firestore()
        .collection("items")
        .whereField("active", isEqualTo: true)
        .order(by: "updated", descending: true)
        .limit(to: 5) //keep track of the last 5 updates
        .addSnapshotListener({ (querySnapshot, error) in
            guard let querySnapshot = querySnapshot else {
                print("Error fetching querySnapshot: \(error!)")
                return
            }
            
            querySnapshot.documentChanges.forEach { (diff) in
                switch diff.type {
                case .added:
                    if let newItem = try docSnapshot?.data(as: Item.self) {
                        //only add new items if these dont exist in the items list yet
                        if !self.items.contains(newItem) {
                            self.items.append(newItem)
                        }
                    }
                case .modified:
                    if let modifiedItem = try docSnapshot?.data(as: Item.self) {
                        let index = self.items.firstIndex(of: modifiedItem) {
                        self.items[index] = modifiedItem
                    }
                case .removed:
                    if let removeItem = try docSnapshot?.data(as: Item.self) {
// 2 Things to check
// 1. Was the item deleted? If yes, then remove the item from the item array 
// 2. If the item doesn't match the query anymore - check if it is just because of the limit which was set - If the limit was surpassed then keep item in array 
                        self.questionItems.remove(object: newItem)
                    }
                }
                self.sortItemsByDate()
                print("Count of items: \(self.questionItems.count)")
            }
            
            self.applySnapshot(items: items, animated: true)
        })
}
首先,我得到了我想要显示的第一批项目(20)。然后,我将添加一个快照侦听器,该侦听器应将项目添加、更新或删除到初始数组中。最初将有5个重复项,因为我在获取前20个项目后调用侦听器。因此,我正在检查。添加该项目是否已存在,如果不存在,则仅将其添加到数组中。对于modiDictions.modified我只是更新相应的项。现在是棘手的部分。当项被删除时,可能是因为1.删除或2.它不再适合查询。如果项被删除,我想将其从数组中删除。但是,如果它被删除是因为它不再适合查询,更具体地说,是因为它超过了查询我想保留它,而不是删除它。 下面是一个数字示例


初始项数组由项1到20[1…20]填充。然后我调用侦听器检查可能发生的任何更新。这将获取前5项,同样是[1…5]。根据我的逻辑,我只是跳过了项目,因为它们已经存在。问题是现在是否添加了新项目。侦听器会将其拾取并添加到数组中,但也会告诉我在达到5的限制时删除一个项目。我无法区分删除(项目实际上已从firestore中删除)和删除(不再适合查询)。我使用的是“FirebaseCore”、“~>6.6.0”

听起来您好像在试图在
处理程序中区分这两种情况。已删除
处理程序:

  • 文档已从查询结果中消失,但仍存在于数据库中
  • 该文档已从数据库中删除,因此也从查询结果中删除

  • 我认为在
    QuerySnapshot
    /
    QueryDocumentSnapshot
    中没有传递任何元数据来区分这两种情况。我唯一能想到的是尝试加载文档,但这将是一个相当昂贵的操作。

    该文档也存在同样的问题。它还说“(删除或不再匹配查询“我仍然看不到区分删除和匹配的方法。在这种情况下:您可以编辑您的问题以显示您的代码以及包含的库的版本吗?谢谢您的编辑,我错过了它。我更新了我的答案。