简体   繁体   English

swift中的完成关闭似乎没有被解雇

[英]Completion closure in swift doesn't appear to be firing

I am following the "Learning Swift" book from O'reilly, and I have some code that looks like: 我正在关注O'reilly的“Learning Swift”一书,我有一些代码如下:

func deleteDocumentAtURL(url: NSURL) {
    NSLog("Got to top of deleteDocumentAtURL, url: \(url)")
    let fileCoordinator = NSFileCoordinator(filePresenter: nil)
    fileCoordinator.coordinateWritingItemAtURL(url, options: .ForDeleting, error: nil, byAccessor: { (urlForModifying) -> Void in
        NSLog("Here I am")
        do {
            NSLog("Got inside deleteDocumentBlock")
            try NSFileManager.defaultManager().removeItemAtURL(urlForModifying)

            // Remove the URL from the list
            self.availableFiles = self.availableFiles.filter {
                $0 != url
            }

            // Update the collection
            self.collectionView?.reloadData()

        } catch let error as NSError {
            let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
}

When I run this code, which is triggered by clicking a button in the interface, the application hangs. 当我运行此代码(通过单击界面中的按钮触发)时,应用程序将挂起。 Also, the log inside the do{} is not firing, which makes me think there's a problem with the whole block? 另外,do {}内的日志没有触发,这让我觉得整个块都有问题? Help appreciated. 帮助赞赏。

Because 因为

let fileCoordinator = NSFileCoordinator(filePresenter: nil)

is local in the function. 在函数中是本地的。 The variable is already destroyed, if you finished the function. 如果您完成了该功能,则该变量已被销毁。 So, the async block can't be executed. 因此,无法执行异步块。

Put fileCoordinator as an instance variable to the class. fileCoordinator作为实例变量放到类中。

You are not handling error and probably have an error 您没有处理错误,可能有错误

Documentation for coordinateReadingItemAtURL : coordinateReadingItemAtURL的文档

outError: On input, a pointer to a pointer for an error object. outError:输入时,指向错误对象指针的指针。 If a file presenter encounters an error while preparing for this read operation, 如果文件演示者在准备此读取操作时遇到错误,

that error is returned in this parameter and the block in the reader parameter is not executed. 在此参数中返回该错误,并且不执行reader参数中的块。

If you cancel this operation before the reader block is executed, this parameter contains an error object on output. 如果在执行读取器块之前取消此操作,则此参数在输出时包含错误对象。

add error handler and see if you get an error 添加错误处理程序,看看是否收到错误

func deleteDocumentAtURL(url: NSURL) {
    NSLog("Got to top of deleteDocumentAtURL, url: \(url)")
    let fileCoordinator = NSFileCoordinator(filePresenter: nil)
    var error: NSError?
    fileCoordinator.coordinateWritingItemAtURL(url, options: .ForDeleting, error: &error, byAccessor: { (urlForModifying) -> Void in
        NSLog("Here I am")
        do {
            NSLog("Got inside deleteDocumentBlock")
            try NSFileManager.defaultManager().removeItemAtURL(urlForModifying)

            // Remove the URL from the list
            self.availableFiles = self.availableFiles.filter {
                $0 != url
            }

            // Update the collection
            self.collectionView?.reloadData()

        } catch let error as NSError {
            let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }

    })
    //Error:
    if(error != nil){
        print(error)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM