【发布时间】:2010-08-13 12:02:26
【问题描述】:
我在回调函数中获得了一些数据,以按顺序写入磁盘,大约每 0.02 秒写入 4KB。如果我从回调函数所在的线程fwrite(),我会在同一线程中的其他代码部分得到一些滞后。因此,为了在写入磁盘时不阻塞该线程,我想将 fwrite 代码移动到另一个线程。问题是回调函数没有实例范围,所以我仅限于调用静态函数或类方法。我要写入的数据属于实例。
我有什么选择?我试过 NSOperationQueue 和 Blocks:
void (^ioBlock)(void) = ^{
fwrite(saveBuffer, length * 4, 1, file);
};
FileSaveOperation *op = [[FileSaveOperation alloc] initWithBlock:ioBlock];
[opQueue addOperation:op];
[op release];
在 FileSaveOperation 中我设置了池:
- (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSRunLoop currentRunLoop];
block();
[pool release];
}
但这给了我这个错误:
_NSAutoreleaseNoPool():__NSFastEnumerationEnumerator 类的对象 0x33ad20 自动释放,没有适当的池 - 只是泄漏
我无法使用performSelector:onThread:,因为我无法访问该实例。我应该让我的数据成员静态吗?有没有更好的解决方案?
【问题讨论】:
-
您的回调没有上下文参数来传递您的实例?
标签: iphone c objective-c multithreading callback