ios - 如何检查 FileManager.default.setUbiquitous() 的结果

标签 ios swift multithreading cocoa

我正在 Swift 中制作一个基于文档的 iOS 应用程序,它使用 FileManager.default.setUbiquitous() 将文件保存到应用程序的 iCloud Drive 文件夹中。现在,我找不到确定 setUbiquitous 何时成功运行的方法(正如我在 this 帖子中提到的)。

我正在浏览 Apple 的文档,它说 setUbiquitous() 的 Objective-C 版本返回了一个 Bool,而

In Swift, this method returns Void and is marked with the throws keyword to indicate that it throws an error in cases of failure.

我想知道此函数何时成功运行,以便我可以运行其他代码来说明上传的文档 - 我发现最接近的方法是在函数的描述中,它说:

use a dispatch queue to call this method from background thread. After the method returns, message your main thread to update the rest of your app’s data structures.

问题是,我不知道“向主线程发送消息以更新应用程序的其余数据结构”是什么意思。对此的任何帮助将不胜感激!

编辑:进一步研究后,我认为我的主要问题是它说“方法返回后...”。如果该方法只返回'Void',我如何才能等到它返回才能更新其余的数据结构?

最佳答案

总体思路如下:

DispatchQueue.global(qos: .background).async {
    // This will run on a background Thread

    let succeeded: Bool
    try {
        FileManager.default.setUbiquitous( ... )
        succeeded = true
    }
    catch {
        // handle error
        succeeded = false
    }

    DispatchQueue.main.async {
        // This will run back on the main thread
        reportResult(succeeded)
    }
}

关于ios - 如何检查 FileManager.default.setUbiquitous() 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147004/

相关文章:

arrays - swift 数组方法 "starts(with:)"导致类型 "Range<Int>"的值没有数字 "starts"

ios - 如何在 BarCharts IOS 图表中创建多个数据集

ios - 是否可以使用iPod开发适用于iOS的应用程序?

ios - 返回时 GestureRecogniser 不工作

ios - 使用函数还是类型方法?

swift - 无法创建 UIImage 并将其附加到数组

ios - 点击时 UIBarButtonItem 不调用另一个类中的函数

java - Java中线程的初始化

multithreading - perl线程异常退出

multithreading - Erlang 怎么 sleep (晚上?)