好的,基本上这就是我想做的:
- 我的文件列表中有一个文件列表应用程序包,例如在文件夹中
myData
(请注意:子文件夹/等中有很多文件/文件夹)
- 我想要复制整个文件树到用户磁盘上的给定位置
- 我需要有访问每个单独的文件被复制,因为其中一些在复制之前需要进行处理。
你会怎么做呢?有任何想法吗?
P.S.
- 我知道关于
NSFileWrapper
's writeFile:atomically:updateFilenames
,但它不允许我访问每个文件
- 我已经有一种获取特定文件夹的文件树的方法。然而,在我看来,它仍然有点慢。有没有任何内置的(或更可可友好的)方法?
- 由于文件相当多,因此必须运行整个过程,以便 GUI 能够执行not freeze
使用 Grand Central Dispatch(GCD)在异步线程中运行方法
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// user disk space - will use user document
NSString *docuemntPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// app bundle folder
NSString *bundleRoot = [[NSBundle mainBundle] resourcePath];
NSString *folderPath = [bundleRoot stringByAppendingPathComponent:@"aFolder"];
NSArray *files = [[NSFileManager defaultManager] enumeratorAtPath:folderPath].allObjects;
for (NSString *aFile in [files objectEnumerator]) {
NSString *copyPath = [folderPath stringByAppendingPathComponent:aFile];
NSString *destPath = [docuemntPath stringByAppendingPathComponent:aFile];
NSError *error;
if ([[NSFileManager defaultManager] copyItemAtPath:copyPath toPath:destPath error:&error]) {
NSLog(@"Copying successful");
// here add a percent to progress view
} else {
NSLog(@"Copying error: %@", error.userInfo);
}
}
// When the copying is finished update your UI
// on the main thread and do what ever you need with the object
dispatch_async(dispatch_get_main_queue(), ^{
[self someMethod:myData];
});
});
并在主线程上使用 UIProgressView 从文件/文件夹的数量指示文件操作的进度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)