2

I want to create pre-seed database at the beginning The file is quite large (5mb).

I use copyItemAtPath to copy files, so do this method has completion? How do i know when this process has been finished?

TomSawyer
  • 3,711
  • 6
  • 44
  • 79
  • 5
    The copy is done when the copyItemAtPath method returns, it is not asynchronous. How else could it report a failure by throwing an error? – Martin R Jun 08 '16 at 11:42

2 Answers2

2

This code is enough:

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)")
}

where destinationPath can be for example:

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true).first
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

From 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
Pang
  • 9,564
  • 146
  • 81
  • 122
Shardul
  • 4,266
  • 3
  • 32
  • 50