更新数据库时,程序会冻结

时间:2013-06-09 14:33:28

标签: iphone ios objective-c core-data nsfilemanager

有一个数据库列出了文档目录中的文件(目标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]]];

}

1 个答案:

答案 0 :(得分:2)

你是什么意思,你无法在后台找到实现这个目标的方法?

这很简单,您可以通过多种不同方式完成。

  • NSOperation
  • Grand Central Dispatch
  • 核心数据新iOS 5 API

一个简单的例子可能是以下(GCD)

dispatch_async(backgroundQueue, ^{
   // create a new context for background execution and assign it the persistent coordinator grabbed form the main context
   NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] init];
   backgroundContext.persistentStoreCoordinator = persistentStoreCoordinator;

   // do your stuff here

   NSError *error;
   if (![backgroundContext save:&error])
   {
      // handle error if necessary
   }
});

其中backgroundQueue是属于GCD的队列,如

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

因此,当您加载应用程序调用{​​{1}}方法时,如上所述。完成后,触发通知以通知用户导入已完成。

在GCD和reloadData之后,您还需要合并背景上下文中的更改。使用父/子上下文的新CD新API不需要这样做。

在下文中,您可以找到有关该主题的一些链接。