NSFileManager - copy files at startup

I need to copy several example files from the application resource folder and put them in the application document folder. I came up with the attached code, it compiles fine, but it does not work. All directories to which I refer exist. I'm not quite sure what I'm doing wrong, can someone point me in the right direction, please?

NSFileManager*manager = [NSFileManager defaultManager]; NSString*dirToCopyTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString*path = [[NSBundle mainBundle] resourcePath]; NSString*dirToCopyFrom = [path stringByAppendingPathComponent:@"Samples"]; NSError*error; NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyFrom error:nil]; for (NSString *file in files) { [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:dirToCopyTo error:&error]; if (error) { NSLog(@"%@",[error localizedDescription]); } } 

EDIT: I just edited the code as it should. Now, however, there is another problem:

2010-05-15 13: 31: 87.7 WriteIt Mobile [4587: 207] DAMutableDictionary.h 2010-05-15 13: 31: 31.795 WriteIt Mobile [4587: 207] FileManager Error: The operation could not be completed. File exists

EDIT: I fixed the problem by giving NSFileManager the names of the copied files.

  [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:[dirToCopyTo stringByAppendingPathComponent:file] error:&error]; 
+4
source share
4 answers

I think the problem is in this line:

 NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyTo error:nil]; 

You specify files in the destination directory instead of the source. Change it to something like:

 NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyFrom error:nil]; 

And everything should be fine.

+5
source

I think the problem is that you are reading files to copy from dirToCopyTo, and I think you meant dirToCopyFrom

Also, to retrieve a document directory, you must use NSDocumentDirectory with - (NSArray *) URLsForDirectory: (NSSearchPathDirectory) inDomains: (NSSearchPathDomainMask) domainMask

+2
source

Please note that long running operations should be avoided at startup:

  • Bad user experience (delay and fraud behavior)
  • Watchdog on iOS can kill your app as if it’s stuck.

So, copy in the secondary stream (or in an operation ... or in any other case a different execution path is used).

Another problem arises if you need data to populate the user interface: in this case:

  • Disable UI Elements
  • Run the async / threaded operation
  • At the end of the copy call (through a notification, protocol .. or other means) to notify the UI, it can begin to receive data.

For example, we copy a ZIP file and unpack it, but it takes some time, so we had to put it in a timer procedure, which will start the interface after completion.

If you need an example, let me know what I know.

PS:
Copying using a ZIP file is more efficient:

  • File system call only
  • Much less bytes to copy

The bad news: you have to use a routine to unzip the zip file, but you can find them on the Internet.

Decompressing Zip files should be more efficient, as these calls are recorded in direct C, and not in Cocoa with all the overhead.

+2
source
  [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:dirToCopyTo error:&error]; 

The destination path is the path to which you want to get a copy, including its file name. You cannot pass the directory path, expecting NSFileManager to populate the name of the source file; he will not do it.

The documentation says that the destination path should not describe anything that exists:

... dstPath must not exist before the operation.

In your case, this is the path to the target directory, so it exists, so the copy does not work.

You need to make the path to the target file by adding the desired file name to it. Then it will not exist (unless previously copied), so the copy will be successful.

+1
source

Source: https://habr.com/ru/post/1309857/


All Articles