[問題] XCode5 測試連續建立UIDocument會失敗

看板MacDev作者 (彼得湯姆)時間11年前 (2013/10/06 04:00), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/4 (看更多)
最近學習用UIManagedDocument來存取core data 並學著寫Xcode5的測試程式 因為saveToURL:forSaveOperation:completionHandler:是非同步的操作 所以加入了__block變數迫使他一定要等到結果回傳回來。 奇怪的是,若這個群組內有三個以上的測試,就一定會錯誤。 錯誤訊息是5, (UIDocumentStateClosed and UIDocumentStateSavingError) 我有確認document檔案位置確定有被刪除 可是若是兩個以下的測試就不會有錯誤。 請問有人有遇到這樣的問題嗎? 以下是我的程式碼: - (void)createTestDocument { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"TestDocument"]; self.document = [[UIManagedDocument alloc] initWithFileURL:url]; __block BOOL waitingForBlock = YES; [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){ if (success) { waitingForBlock = NO; NSLog(@"Create document successfully"); } else { XCTFail(@"can't create"); waitingForBlock = NO; } }]; while(waitingForBlock) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; } } - (void)setUp { [super setUp]; [self createTestDocument]; } - (void)tearDown { NSError *error; BOOL retult = [[NSFileManager defaultManager] removeItemAtURL:[self.document fileURL] error:&error]; [super tearDown]; } - (void)testNothing { if (UIDocumentStateNormal != [self.document documentState]) { XCTFail(@"document is not normal, cannot test\ndocument state:%u", [self.document documentState]); } } - (void)testNothing1 { if (UIDocumentStateNormal != [self.document documentState]) { XCTFail(@"document is not normal, cannot test\ndocument state:%u", [self.document documentState]); } } - (void)testNothing2 { if (UIDocumentStateNormal != [self.document documentState]) { XCTFail(@"document is not normal, cannot test\ndocument state:%u", [self.document documentState]); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.224.193.165
文章代碼(AID): #1IK6_hPD (MacDev)
文章代碼(AID): #1IK6_hPD (MacDev)