0

我是 Xcode 和编程语言的新手,我需要你的帮助。我正在处理消息应用程序,但无法使用完成处理程序块。这是我的代码:

     @IBAction func SendButton(_ sender: AnyObject) {


    if self.textfield.text != "" {


        let mess = CKRecord(recordType: "Message")
        mess["content"] = textfield.text as CKRecordValue?

        let publicdata = CKContainer.default().publicCloudDatabase

        publicdata.save(mess, completionHandler: {(record, error) -> Void in
            if error == nil {


                let indexpath = NSIndexPath(item: self.chat.count, section: 0)


                CATransaction.begin()
                CATransaction.setDisableActions(true)


                self.CollView.performBatchUpdates ({

                    self.chat.insert(mess, at: self.chat.count)
                    self.CollView.insertItems(at: [indexpath as IndexPath])


                }, completion: {(true) -> Void in

                print("Animation completed")
                self.CollView.contentOffset = CGPoint(x: 0, y: 40)

                })

                CATransaction.commit()

            print("SAVED")


            }else{
            print("error")
            }})

    }

    textfield.text = ""
}

我使用CATransaaction 执行BatchUpdates,但是performBatchUpdates 方法中的Completion Handler Block 无法完成。如您所见,这不是正确的编写方式,我知道,但是我已经尝试了所有我知道的方法来实现它,但它不会。完成 :

 completion: {(true) -> Void in

            print("Animation completed")
            self.CollView.contentOffset = CGPoint(x: 0, y: 40)

            })

请帮我。谢谢 !!

4

2 回答 2

3

你可以试试这个

collectionView?.performBatchUpdates({
                print("First part")
            }, completion: { (result: Bool) in
                print("Second part")
            })
于 2016-12-22T06:03:15.760 回答
0
 func loadData() {
    chat = [CKRecord]()


let publicData = CKContainer.default().publicCloudDatabase


    let query = CKQuery(recordType: "Message", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
    query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    publicData.perform(query, inZoneWith: nil, completionHandler: {(results, error) -> Void in

        if let text = results {
        self.chat = text
            DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { () -> Void in
                self.CollView.reloadData()
            }


        }else{
        print("error")
        }


    })

}
于 2016-12-23T19:05:33.553 回答