NSOperation 和 fwrite (Iphone)

发布于 2024-08-24 12:33:37 字数 1093 浏览 3 评论 0原文

我对这段代码有问题。基本上我想从定时器函数异步执行 fwrite 。

这是我的计时器函数中的代码块。 (这将由计时器每 0.2 秒调用一次。WrtiteFilerOperation

-(void)timerFunction
{

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

是一个 NSOperation 类,它必须将传递的缓冲区写入文件。 我在 WriteFileOperation 的“start”方法中添加了这段代码。

- (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];
}

这里的问题是,我的timerFunction被NSOperation阻塞,直到它将缓冲区写入文件。(我的意思是阻塞直到start方法完成执行) 性能看起来和直接将fwrite放在timerFunction中是一样的。

我想返回到timerFunction,而不是等待start 方法执行完成。

我在这里做错了什么?

提前致谢

拉古

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 NSOperation 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-08-31 12:33:37

您的操作正在阻塞,因为您强制该操作在主线程上运行其 start 方法,从而消除了使用 NSOperationQueue 的任何好处。相反,我建议删除上面看到的 -start 实现,并将代码移至 -main

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

应在某些时候删除 NSLog 以提高性能。如果您执行的操作不止于此,您可能还需要将上述内容包装在 NSAutoreleasePool 中。

为了防止多个线程同时写入文件,您应该使用如下代码将最大并发操作数设置为1:

[_queue setMaxConcurrentOperationCount:1];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文