作者petertom (彼得汤姆)
看板MacDev
标题[问题] XCode5 测试连续建立UIDocument会失败
时间Sun Oct 6 04:00:39 2013
最近学习用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