-1

書き込み中にアプリがフリーズするため、スレッドにファイルを書き込もうとしましたが、書き込みプロセスを起動するとクラッシュします

2011-10-04 21:53:51.022 xxxxxxxxx[2046:6603] *** Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: '*** -[NSConcreteFileHandle seekToEndOfFile]: Operation timed out'

私のコード:

- (void)WriteTest{
    [NSThread detachNewThreadSelector:@selector(DoWriteTest:) toTarget:self withObject:hFile];
}

- (void)DoWriteTest:(NSFileHandle *)aHandle{
    int i;

    if (aHandle)   {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        NSLog(@"---start--- writing test file --");
        for (i=0; i<1024*1024; i++) {
            [aHandle seekToEndOfFile];
            [aHandle writeData:[NSData dataWithBytes:bytes length:(sizeof bytes) - 1]];
            usleep(1);
        }

        NSLog(@"---end--- writing test file");

        [pool release];
    } else {
        NSLog(@"ERROR: writing test file thread");
    }
}

スレッドなしでこれを行ったとき、このコードは機能します。何が間違っているのか説明してください。Google検索をたくさん行いますが、解決策が見つかりません。ありがとう。

4

1 に答える 1

0

あなたのコードは私のために働いた。私の推測では、ファイル ハンドルを正しく開いていません。[pool drain]の代わりに を使用する必要があることにも注意してください[pool release]。私の完全なコードは以下のとおりです。質問があればコメントしてください。それが役に立てば幸い。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/Users/Drew/Desktop/test.txt"];
    [NSThread detachNewThreadSelector:@selector(threadSelector:) toTarget:self withObject:fileHandle];
}

- (void)threadSelector:(NSFileHandle *)aHandle {
    if (aHandle)   {
         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
         int i;
         for (i=0; i<1024; i++)
         {
             [aHandle seekToEndOfFile];
             char buffer[1024] = "a string ";
             [aHandle writeData:[NSData dataWithBytes:buffer length:(sizeof buffer) - 1]];
             usleep(1);
         }
         [pool drain];
    }
}
于 2011-10-04T20:50:13.117 に答える