2012-09-15 78 views
3

我遇到了一個奇怪的問題。我確信我在代碼中的其他地方做了一些事情,並且它沒有正確關閉或者其他東西,但現在它處於報告爲關閉狀態,但是當我調用OpenWithCompletionHandler時,它永遠不會返回。見下:UIManagedDocument OpenWithCompletionHandler永不返回

//if the file is closed, open it and then set up the controller 
    if (file.documentState == UIDocumentStateClosed){ 
     //---- this code executes   
     [file openWithCompletionHandler:^(BOOL success){ 
      // ---- this code NEVER executes 
     }]; 
    } 

任何想法?

+0

你有沒有搞清楚是什麼問題?在升級到6.0 SDK後,我無法使用openWithCompletionHandler在iOS 5.1中工作。它永遠不會返回。 –

+0

沒有。我幾個星期沒有看到這個問題,但我現在也在iOS6上。 –

+0

我有同樣的問題...使用XCode 4.5.2(4G2008a),使用iOS模擬器6.0,但使用iOS 5.1操作系統... openWithCompletionHandler永遠不會觸發封閉的數據庫。我有一個HUD在完成射擊時降低,因此HUD保持不變,這在模擬器中顯然是很痛苦的。 – Joe

回答

0

請參閱Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument

我的解決方案和那些報告一樣,但我不得不將我的應用的部署目標降低到iOS 5.0,以便「iPhone 5.0 Simulator」可用作運行目標。我只看到這個問題試圖使用iPhone 5.1模擬器與XCode 4.5.2,5.0和6.0模擬器工作。

0

我遇到了同樣的問題。

您是否試圖在viewDidLoad內打開文檔?

嘗試將代碼移至其他方法。它解決了我的問題。

在ViewController.h

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic,strong) UIManagedDocument *document; 

在ViewController.m

@synthesize managedObjectContext = _managedObjectContext; 
@synthesize document = _document; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do not try to open the document here 
    // Call another method instead :D 
    if (!_managedObjectContext) { 
     [self createContext]; 
    } 
} 

- (void)createContext 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSURL *url = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
    url = [url URLByAppendingPathComponent:@"Database"]; 

    self.document = [[UIManagedDocument alloc] initWithFileURL:url]; 

    // FILE DOES NOT EXIST - Let's create a new one  
    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) { 
     [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { 
      if (success) { 
       self.managedObjectContext = self.document.managedObjectContext; 
      } else { 
       NSLog(@"ERROR: Cannot create new document"); 
      } 
     }]; 

    // FILE IS CLOSED - Let's open it 
    } else if (self.document.documentState == UIDocumentStateClosed) { 
     [self.document openWithCompletionHandler:^(BOOL success) { 
      if (success) { 
       self.managedObjectContext = self.document.managedObjectContext; 
      } else { 
       NSLog(@"File is closed and it wont open!"); 
      } 
     }]; 

    // FILE EXISTS AND IS OPENED - Yay! 
    } else { 
     self.managedObjectContext = self.document.managedObjectContext; 
    } 
}