iOS - Swift 我如何知道 copyItemAtPath 何时完成?

iOS - Swift How do i know when copyItemAtPath has been finished?

我想在开始时创建预种子数据库 文件很大 (5mb)。

我用copyItemAtPath复制文件,请问这个方法有补全吗? 我怎么知道这个过程何时完成?

这段代码就够了:

do {
    // copy files from main bundle to documents directory
    print("copy")
    try 
        NSFileManager.defaultManager().copyItemAtPath(sourcePath, toPath: destinationPath)
} catch let error as NSError {
     // Catch fires here, with an NSError being thrown
     print("error occurred, here are the details:\n \(error)")
}

destinationPath 可以是例如:

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true).first

来自How to show the progress of copying a large file in iOS?

  1. Run your copying process in a seperate thread (T1)
  2. Run another thread (T2) which reads periodically (say every 100ms) the destination file current_size.
  3. Calculate the percentage to display progress: current_size / total_size