2012-07-12 38 views
0

所以我知道你應該在主線程上做你的更新。我使用蘋果的代碼寫我們的數據庫做文件目錄:調試主線程iPhone上的UI更新,活動指示器不旋轉

// Creates a writable copy of the bundled default database in the application Documents directory. 
- (void)createEditableCopyOfDatabaseIfNeeded { 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) 
     return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

我做

[activityIndicator startAnimating]; 
[self createEditableCopyOfDatabaseIfNeeded]; 

我沒有看到我的活動指示燈。如果我註釋掉[self createEditableCopyOfDatabaseIfNeeded];,我確實看到了我的活動指標。有沒有一種方法來調試,看看發生了什麼,爲什麼我沒有看到我的旋轉指標?謝謝。

+0

你應該這樣做**在主線程上更新UI **。如果您有大量非UI工作,則可以使用performSelectorInBackground在後臺執行此操作,或者可以使用GCD和塊,以稍後在UI線程上或在後臺 – Nate 2012-07-12 23:18:16

回答

2

從上下文來看,很難說,但我假設您希望活動指示符代表createEditableCopyOfDatabaseIfNeeded正在進行的操作。假設你的主線程調用了[self createEditableCopyOfDatabaseIfNeeded],你可以這樣做:

[activityIndicator startAnimating]; 
dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
    [self createEditableCopyOfDatabaseIfNeeded]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [activityIndicator stopAnimating]; 
    }); 
}); 
+0

括號中和括號沒有正確關閉。有一個失蹤)和; – iosMentalist 2013-03-20 09:37:19

+0

不錯,謝謝!我想我已經習慣使用諸如Xcode和Eclipse之類的IDE了:) – Krumelur 2013-04-01 12:51:52