Firebase 实时数据库 - 无效的 NSError 实例



运行此代码后,我在控制台中收到一条奇怪的消息:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let pickUp = sortedPickUps[indexPath.item]
let alert = UIAlertController(title: "Delete", message: "Permanently delete your order?", preferredStyle: .alert)
let yes = UIAlertAction(title: "Yes", style: .destructive) { (actio) in
DispatchQueue.main.async {
self.pickUpController.delete(pickUp: pickUp)
self.pickUpController.deleteFirebasePickUp(pickUp: pickUp, completion: { (error) in
if let error = error {
NSLog("Error deleting pick up (error)")
}
})
self.sortedPickUps = self.pickUpController.pickUps.sorted(by: { $0.timestamp > $1.timestamp })
self.collectionView.reloadData()
}
self.sendNotification()
}
let no = UIAlertAction(title: "No", style: .default) { (action) in }
alert.addAction(yes)
alert.addAction(no)
present(alert, animated: true, completion: nil)
}

deleteFirebasePickUp中的URLSession如下所示

URLSession.shared.dataTask(with: request) { (data, _, error) in
if let error = error {
NSLog("Error deleting entry from server: (error)")
DispatchQueue.main.async {
completion(error)
}
return
}
DispatchQueue.main.async {
guard let index = self.pickUps.index(of: pickUp) else {
NSLog("Something happened to the entry")
completion(NSError())
return
}
self.pickUps.remove(at: index)
completion(nil)
}
}.resume()

该应用程序不会崩溃,实际上会执行它应该执行的操作 - 从设备和数据库中删除pickUp。错误是error = (Error?) domain: nil - code:0

调试器说

Something happened to the entry 2018-10-13 14:14:37.608435-0700 chute[36293:4218837] -[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.

有什么想法吗?

重读调试器的消息:

[NSError init] called; this results in an invalid NSError instance.

如果您重新读取第二个代码块

DispatchQueue.main.async {
guard let index = self.pickUps.index(of: pickUp) else {
NSLog("Something happened to the entry")
completion(NSError()) // THIS LINE
return
}
self.pickUps.remove(at: index)
completion(nil)
}

NSError()构造函数对应于[NSError init],已被弃用。应改用init(domain:code:userInfo:)初始值设定项:https://developer.apple.com/documentation/foundation/nserror/1417063-init

最新更新