-1

I attempt to write a file in thread because it freeze my app when writing but when i launch writing process it crash

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

my code:

- (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");
    }
}

when i did this without a thread this code work, can you explain me what i'm wrong please, i make a lot of google search but i don't find a solution. Thanks.

sidyll
  • 57,726
  • 14
  • 108
  • 151
doc
  • 266
  • 2
  • 15

1 Answers1

0

Your code worked for me. My guess is you opened your file handle incorrectly. Also note that you should use [pool drain] instead of [pool release], but this is nitpicking. My full code is below. Comment if you have any questions. Hope it helps.

- (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];
    }
}
Andrew
  • 4,145
  • 2
  • 37
  • 42
  • thanks drew for ypur reply, i test your code and it worked for me to with the same handle, but when i modify the loop in your code with this `code`for (i=0; i<1024*1024; i++)`code` it crash, i think the problem is the loop but i don't see what is wrong – doc Oct 04 '11 at 21:26
  • 1024^2 is a pretty big number. How big is your buffer `bytes`? For me, it's not freezing my app, it's freezing any application I try to open the file with, and my buffer is only eight characters. Perhaps you should reevaluate and find a more efficient way to achieve what you're doing than writing something one million times in a matter of one second. If this is necessary, you're probably going to need some way of making sure that your operations with this file occur more slowly and avoid maxing out the RAM or CPU. I must say, this is the worst my MacBook Pro has ever performed. That's special. – Andrew Oct 04 '11 at 23:07
  • Thanks Drew, i Wang to create a file with random size (1Mo to 23Go) and fill it with some data, the size of my buffer is 4bytes, i choose this way to perform this because i don't want to use a lot of ram, if it tale long time to do, it's not a problem. – doc Oct 05 '11 at 07:06
  • For more informations, i want to create a file with random size and fill it with zero.. – doc Oct 05 '11 at 07:13
  • finally i solve my problem using C functions instead of NSFileHandle – doc Oct 16 '11 at 15:25