开发者

NSOperation and fwrite (Iphone)

I am having problem with this code.Basically I want to execute the fwrite from a timer function asyncronusly.

Here is the code block in my Timer function. (This will call by the timer every 0.2 seconds.

-(void)timerFunction
{

       WriteFileOperation * operation =
       [WriteFileOperation writeFileWithBuffer:pFile buffer:readblePixels length:nBytes*15];
       [_queue addOperation:operation]; // Here it is waiting to complete the fwrite
}

The WrtiteFilerOperation is an NSoperation class which it has to write the passing buffer to a file. I added this code in WriteFileOperation's "start" method.

- (void)start

{

    if (![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }


    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];

    NSLog(@"write bytes %d",fwrite(_buffer, 1, _nBytes, _file));
    free(_buffer);
    [self finish];
}

The problem here is , my timerFunction blocked by NSOpe开发者_开发百科ration until it writes the buffer into file.(I mean blocked till start method finishes its execution) and the performance seems same as directly placing the fwrite in timerFunction.

I want to just return to timerFunction with out waiting from the start method execution to be completed.

What I am doing wrong here ?

Thanks In Advance

Raghu


Your operation is blocking because you are forcing the operation to run its start method on the main thread, removing any benefits of using an NSOperationQueue. Instead, I'd recommend removing the -start implementation you see above and moving the code to -main:

- (void)main
{
    NSLog(@"write bytes %d",fwrite(_buffer, 1, _nBytes, _file));
    free(_buffer);
    [self finish];
}

The NSLog should be removed at some point to improve performance. You may also need to wrap the above in an NSAutoreleasePool if you do more than this.

In order to prevent multiple threads from writing to the file at the same time, you should set the maximum number of concurrent operations to 1 using code like the following:

[_queue setMaxConcurrentOperationCount:1];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜