Я предлагаю вам сделать вашу обработку БД в фоновом режиме. Возможно, вы можете отключить интерфейс или отобразить индикатор ожидания, пока вы обновляете базу данных в фоновом потоке. После этого вы можете включить интерфейс или скрыть индикатор.
Существуют разные способы создания фонового потока.
- Создать тему вручную, используя
NSThread
class
- Использование
NSOperation
и NSOperationQueue
классов
- Использование Grand Central Dispatch (GCD)
Надеюсь, это поможет.
Редактировать
Здесь простой код для вашей цели (после предложения @JeremyP).
Сначала создайте NSOperation
подкласс
// .h
@interface YourOperation : NSOperation
{
}
//.m
@implementation YourOperation
// override main, note that init is executed in the same thread where you alloc-init this instance
- (void)main
{
// sorround the thread with a pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// do your stuff here..
// you could send a notification when you have finished to import your db,
// the notification is sent in a background thread,
// so in the place where you listen it, if you need to update the interface,
// you need to do it in the main thread (e.g. performSelectorOnMainThread)
[[NSNotificationCenter defaultCenter] postNotificationName:kImportComplete object:self];
[pool drain];
pool = nil;
}
Затем, например, в вашем делегате приложения вызовите [self import];
, который можно определить следующим образом:
if (!(self.operationQueue)) {
NSOperationQueue* q = [[NSOperationQueue alloc] init];
[q setMaxConcurrentOperationCount:1];
self.operationQueue = q;
[q release];
YourOperation *op = [[YourOperation alloc] init];
[self.operationQueue addOperation:op];
[op release], op = nil;
}