【问题标题】:Firestore interferes with animationFirestore 干扰动画
【发布时间】:2020-08-23 17:40:51
【问题描述】:

我的 Swift 项目有一些问题。 Firestore 函数似乎干扰了我试图在单元格中执行的动画。

我的目标

This animation of a progress bar going from 0.0 to 1.1 inside a UITableViewCell (screenrecord)

实际结果

目前,动画不工作。进度条正在更新,但没有动画。 It looks like this now.

问题

目标和实际结果在TaskCell.swift 中都有相同的动画代码。当与startActionII() 一起单击该框时,startAnimation() 会被调用。

@IBAction func checkBoxAction(_ sender: Any) {
    startActionII()
    startAnimation()
}

func startAnimation() {
    UIView.animate(withDuration: 6.0) {
        self.progressBar.setProgress(1.0, animated: true)
    }
}


func startActionII() {
    if items![indexRow!].checked {
        delegate?.changeButton(state: false, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
    } else {
        delegate?.changeButton(state: true, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
    }
}

这就是问题所在。委托方法被正确调用:所有打印语句和 Firestore 更新都有效。但是,Firestore 代码似乎会干扰动画。当我使用下面的代码changeButton() 时,动画不起作用。但是,当我用打印语句替换所有 Firestore 代码时,动画确实可以工作。这是我的changeButton 委托方法。

func changeButton(state: Bool, indexSection: Int?, indexRow: Int?, itemID: String?) {
    
    print("Doing logic II...")
    
    sections[indexSection!].items[indexRow!].checked = state

    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.impactOccurred()

    if let itemID = itemID {
        let itemRef = db.collection(K.FStore.lists).document(currentListID!).collection(K.FStore.sections).document("\(indexSection!)").collection(K.FStore.items).document(itemID)



         if sections[indexSection!].items[indexRow!].checked {
            itemRef.updateData([
                 K.Item.isChecked: true,
                 K.Item.checkedBy: currentUserID!,
                 K.Item.dateChecked: Date()
            ]) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }

            print("hello")


                    DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                        print("Moving item to itemsChecked...")

                        if let indexSection = indexSection, let indexRow = indexRow {
                                                            let item = self.sections[indexSection].items[indexRow]

                            // Item's properties variables
                            let itemID: String
                            let name: String
                            let uid: String
                            let isChecked: Bool
                            let checkedBy: String
                            let dateCreated: Date
                            let dateChecked: Date

                            let itemRef = self.db.collection(K.FStore.lists).document(self.currentListID!).collection(K.FStore.sections).document("\(indexSection)").collection(K.FStore.items).document(item.itemID!)

                            itemRef.getDocument { (document, error) in
                                if let document = document, document.exists {
                                    let dataDescription = document.data().map(String.init(describing:)) ?? "nil"


                                    // Get the properties of the item
                                    let name = document.data()?[K.Item.name] as? String
                                    let uid = document.data()?[K.Item.uid] as? String
                                    let category = document.data()?[K.Item.categoryNumber] as? Int
                                    let isChecked = document.data()?[K.Item.isChecked] as? Bool
                                    let dateCreated = document.data()?[K.Item.date] as? Date
                                    let dateChecked = document.data()?[K.Item.dateChecked] as? Date
                                    let checkedBy = document.data()?[K.Item.checkedBy] as? String

                                    var ref: DocumentReference? = nil

                                    // Save the properties of the item in sectionsDeleted
                                    ref = self.db.collection(K.lists).document(self.currentListID!).collection(K.FStore.sectionsChecked).document("\(category!)").collection(K.FStore.items).addDocument(data: [
                                            K.Item.name: name,
                                            K.Item.isChecked: isChecked,
                                            K.Item.categoryNumber: category,
                                            K.Item.date: dateCreated,
                                            K.Item.dateChecked: dateChecked,
                                            K.Item.checkedBy: checkedBy,
                                            K.Item.uid: uid,
                                            K.Item.dateDeleted: Date()
                                    ]) { err in
                                        if let err = err {
                                            print("Error adding document: \(err)")
                                        } else {

                                            // If successfull, delete the item in the normal collection
                                            itemRef.delete() { err in
                                                if let err = err {
                                                    print("Error removing document: \(err)")
                                                } else {
                                                    print("Document successfully removed!")
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    print("Document does not exist")
                                }
                            }

                            self.tableView.reloadData()

        } else {
            itemRef.updateData([
                K.Item.isChecked : false
            ]) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }
                            }
    }
                } } } }

    refreshTable()
}

谁能帮我解决这个问题?

非常感谢, 马特

GitHub link & Link to branch 'MoveSections'

【问题讨论】:

  • 我下载了这个项目,但是在那儿找不到这个代码,什么分支?您是否提交了更改?
  • @Alex.Pinhasov,你是对的,对不起。 This is the link to the branch。最新提交在分支“MoveSections”上称为“Stackoverflow”

标签: ios swift firebase animation google-cloud-firestore


【解决方案1】:

我在你的项目中发现了一些问题

  1. google info plist 的路径错误,您创建了对 is 的引用,但您应该已将其复制到您的项目路径中。

我无法构建项目,因为我缺少我提到的 google 信息 plist

*编辑

看看你在这里做了什么

您调用“startActionII()”并更改模型布尔状态,这很好,然后在该异步块中,您调用表视图上的重新加载数据。

您有相互冲突的 UI 行为,一方面您试图显示带有动画的进度条,同时(几乎是异步的)您正在重新加载整个表格视图并覆盖您的动画。

您所做的事情是有问题的,因为您的 UI 是交互式的,这意味着用户点击会立即影响数据库,而不是在流程结束时影响数据库,那么进度动画的目的是什么?

【讨论】:

  • 谢谢。我已经更新了 GitHub 存储库,因此它现在应该包含 plist 文件。当谈到 DispatchQueue 和异步/同步时,我是一个完整的初学者,所以我正在努力解决这个问题。动画的目的是用户有 5 秒的机会撤消 checkBoxAction,之后单元格消失。
  • 那么你应该做的是: 1. 启动进度动画 + 更新模型 2. 设置一个计时器或动画完成块来知道什么时候过去了 5 秒 3. 如果用户没有撤消他的选择,然后才进行 firebase DB 调用 4. 请记住这一点 - 如果您期望在单个单元格中进行更改,请找到该单元格并为他更新 UI,或者使用更新单元格作为索引路径而不是重新加载整个表。
猜你喜欢
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多