有一个数据库列出了文档目录中的文件(目标 c - 核心数据)。每次打开程序时都会更新此列表。对于 700 个文件,程序需要 30 秒才能打开,并且在此期间单击按钮完全没有响应。
我找不到在后台执行此操作的方法。有没有办法慢慢强制处理器(cpu)?因此,当用户继续使用该程序时,让它在后台更新。
- (void)Reload_Data
{
id delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:kDocdir];
for (NSString *pathi in directoryEnumerator){
NSString *path = [self kDoc_dosya:pathi];
NSError *error_iki;
NSDictionary *file_propery = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error_iki];
if (!error_iki && ![[path lastPathComponent] hasPrefix:@"."] && [file_propery fileType] != NSFileTypeDirectory) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:Parcalar_s inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"url LIKE [cd] %@",[self Doc_Sil:path]];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:1];
[fetchRequest setFetchLimit:1];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (!error && [fetchedObjects count] < 1) {
Parcalar *parca = (Parcalar *)[NSEntityDescription insertNewObjectForEntityForName:Parcalar_s inManagedObjectContext:context];
parca.url = [self Doc_Sil:path];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
NSUInteger suresi = CMTimeGetSeconds(asset.duration);
parca.sure = [NSNumber numberWithInteger:suresi] ;
}
}
}
NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) [self Alert_Uyari:[NSString stringWithFormat:@"Unresolved error %@, %@", error, [error userInfo]]];
}