- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个正在重构的应用程序,我刚刚实现了多线程,以便 UI 可以运行得更流畅。在 iPhone 模拟器中,我没有遇到任何泄漏,但在运行 iOS 4.2 的 iPhone 3G 上进行测试时,出现了内存泄漏。我已经做了很多搜索,寻找使用操作队列实现自动释放池的正确方法,非常感谢您的帮助。
我在我的 View Controller 中创建了一个 nsoperationqueue
- (void)loadData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSOperationQueue *queue = [NSOperationQueue new]; // creates multithread for loading data without slowing UI
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(firstRun) object:nil];
[queue addOperation:operation];
[operation release];
[queue release];
[pool release];
}
最佳答案
首先,您不应该只是创建然后释放队列。更自然的做法是创建该队列作为类的 ivars 之一,然后在 View Controller 消失时释放它(您还可以取消任何挂起的操作并取消/等待任何正在运行的操作完成)。
其次,您不需要在创建操作并将其添加到队列中的方法中使用自动释放池,因为该方法是从主线程调用的。相反,您需要对象实际调用的方法中的自动释放池(这可能在另一个线程上运行)。
因此,您可能有以下内容(假设您将队列命名为 ivarqueue_):
- (void)viewDidLoad
{
[super viewDidLoad];
if( !queue_ ) queue_ = [[NSOperationQueue alloc] init];
// other view loading code
}
- (void)loadData
{
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(firstRun) object:nil];
[queue_ addOperation:operation];
[operation release];
}
- (void)firstRun
{
// Here we may run on another thread, so 1) we need an autorelease pool; and
// 2) we need to make sure we don't do anything that requires a runloop
NSAutoreleasePool* threadPool = [NSAutoreleasePool new];
// do long-running things
[threadPool drain];
}
- (void)dealloc
{
if( queue_ ) {
[queue_ setSuspended:YES];
[queue_ cancelAllOperations];
// you need to decide if you need to handle running operations
// reasonably, but don't wait here because that may block the
// main thread
[queue_ release];
}
// other dealloc stuff
[super dealloc];
}
您还可以根据需要初始化队列,因此不必在 viewDidLoad 中初始化,而是检查队列是否存在,并在需要时在要添加操作的任何位置进行初始化。这可能包含在它自己的方法调用中,但这里的延迟初始化可能并不是真正必要的,因为队列非常轻量级。
关于iphone - 如何正确使用 nsoperationqueue 的 autoreleasepool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557537/
我在 crashlytics 中得到了一些我完全不明白的崩溃报告,这是崩溃线程的崩溃日志: 我没有找到任何关于我的代码的提示,它也不是可重现的或只发生在特定设备上的东西。根据 Crashlytics
我需要从 JSON 下载数据,并将数据分配给 NSOperationQueue 之外的 NSData。这是我的代码: -(void)parsingInfo { NSURL *url = [NS
我有一个应用程序,其中一个长时间运行的进程(> 1 分钟)被放置在 NSOperationQueue(队列 A)上。当队列 A 操作运行时,UI 完全响应,完全符合预期。 但是,我有一种用户可以执行的
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我有几个后台任务需要处理,同时仍保持 UI 响应。我开始走上创建和管理线程的道路,但很快就遇到了 NSOperations。听起来是一个更好的解决方案。 。 . 但是,我似乎无法获得对 NSOpera
我想知道释放 ivar NSOperationQueue 的正确方法是什么,以防它仍有一些操作正在运行,这种情况通常在用户突然退出应用程序时发生。在一些示例中,我看到使用了 waitUntilAllO
我正在努力解决这个问题,所以我的问题是: 当我像这样实例化 NSOperationQueue 时: NSOperationQueue * operationQueue = [[NSOperationQ
我第一次尝试基于 Swift/NSOperationQueue 的设计,我正在尝试找出如何维护跨队列的数据完整性。 我处于设计过程的早期阶段,但该架构可能会涉及一个队列(称为sensorQ),用于处理
我正在寻找一种重新排序 NSOperationQueue 的方法。 我可以取消所有操作并按照我想要的顺序将它们扔回去,但我希望有一些更干净的东西。有什么想法吗? 最佳答案 NSOperationQue
我是iPhone新手。在哪里可以找到NSOperationQueue和NSOperation的示例?NSOperationQueue和NSOperation与线程相比有什么优势? 谢谢 最佳答案 阅读
正如 NSOperationQueue 类引用所述:在 iOS 中,操作队列不使用 Grand Central Dispatch 来执行操作。它们为非并发操作创建单独的线程,并从当前线程启动并发操作。
我创建了一个NSOperationQueue子类,将maxConcurrentOperations设置为1,并将addOperation方法重写为以下内容: -(void)addOperation:(
我一直在使用 NSOperationQueue,但我遇到了非常奇怪的内存问题。我已经尝试将问题减少到最简单的问题,在这里我得到了: 在初始化中: _queue = [[NSOperationQueue
我最近开始学习 Swift 中的多线程,我想使用 NSOperationQueue 和 NSOperation 类尝试一些基本的多线程。 在 .swift 文件的全局范围内(在 Xcode 中的命令行
我的任务是清理项目中一些现有的 Swift 代码,这些代码刚刚转换为 Swift 3。但是,我不断看到这个对我来说看起来很可疑的代码。 OperationQueue().addOperation(so
我正在开发一个应用程序,该应用程序从使用 performSelectorInBackground 启动的单独线程中的循环调用 AudioServicesPlayAlertSound(kSystemSo
我在我的应用程序中使用了 NSOperation 子类,它将在单个操作中执行以下 4 个任务,我希望所有这 4 个任务都在后台线程上运行,所以我将其封装到单个 NSOperation 类,这样我就可以
我在暂停当前正在执行的任务时遇到问题,我尝试设置 NSOperationQueue setSuspended=YES 暂停和 setSuspended=NO恢复进程。 根据 apple docs我不能
我正在使用 NSOperationQueue 执行并发下载并插入到核心数据中。 下面是代码,我正在使用 if(nil==queue) { queue=[[NSOperationQueue al
我正在为 ios 进行调整(显示来自网络的一些图像),并且我正在尝试使用 NSOperationQueue。首先,我在主线程上运行了一些代码来看看它是什么样子。显示每个图像持续了 ±5 秒(UI 卡住
我是一名优秀的程序员,十分优秀!