gpt4 book ai didi

iphone - 如何防止IOS并发NSOperation在主线程上运行

转载 作者:可可西里 更新时间:2023-11-01 05:42:15 28 4
gpt4 key购买 nike

我正在编写一个应用程序,它使用 ASI HTTP 定期从 Web 服务器获取数据,然后处理该数据以在 UI 上显示与用户相关的内容。从单个服务器上的不同请求中检索数据。数据本身需要按特定顺序处理。其中一个数据 block 比其他数据 block 大得多。

为了在处理数据时不锁定 UI,我尝试使用 NSOperationQueue 在不同的线程上运行数据处理。这在大约 90% 的情况下工作正常。然而,在剩下的 10% 的时间里,最大的数据 block 正在主线程上处理,这导致 UI 阻塞 1-2 秒。该应用程序在不同的选项卡中包含两个 MKMapView。当加载两个 MKMapViews 选项卡时,在主线程上处理最大数据 block 的时间百分比增加了 50% 以上(这似乎表明当有更多并发事件时会发生这种情况)。

有没有办法阻止 NSOperationQueue 在主线程上运行代码?

我尝试使用 NSOperationQueue –setMaxConcurrentOperationCount:,增加和减少它,但在这个问题上没有真正的改变。

这是启动定期刷新的代码:

- (void)refreshAll{    

// Create Operations
ServerRefreshOperation * smallDataProcessor1Op = [[ServerRefreshOperation alloc] initWithDelegate:_smallDataProcessor1];
ServerRefreshOperation * smallDataProcessor2Op = [[ServerRefreshOperation alloc] initWithDelegate:_smallDataProcessor2];
ServerRefreshOperation * smallDataProcessor3Op = [[ServerRefreshOperation alloc] initWithDelegate:_smallDataProcessor3];
ServerRefreshOperation * smallDataProcessor4Op = [[ServerRefreshOperation alloc] initWithDelegate:_smallDataProcessor4];
ServerRefreshOperation * smallDataProcessor5Op = [[ServerRefreshOperation alloc] initWithDelegate:_smallDataProcessor5];
ServerRefreshOperation * hugeDataProcessorOp = [[ServerRefreshOperation alloc] initWithDelegate:_hugeDataProcessor];

// Create dependency graph (for response processing)
[HugeDataProcessorOp addDependency:smallDataProcessor4Op.operation];
[smallDataProcessor5Op addDependency:smallDataProcessor4Op.operation];
[smallDataProcessor4Op addDependency:smallDataProcessor3Op.operation];
[smallDataProcessor4Op addDependency:smallDataProcessor2Op.operation];
[smallDataProcessor4Op addDependency:smallDataProcessor1Op.operation];

// Start be sending all requests to server (startAsynchronous directly calls the ASIHTTPRequest startAsynchronous method)
[smallDataProcessor1Op startAsynchronous];
[smallDataProcessor2Op startAsynchronous];
[smallDataProcessor3Op startAsynchronous];
[smallDataProcessor4Op startAsynchronous];
[smallDataProcessor5Op startAsynchronous];
[hugeDataProcessorOp startAsynchronous];
}

这是设置启动数据处理的 ASI HTTP 完成 block 的代码:

[_request setCompletionBlock:^{
[self.delegate setResponseString:_request.responseString];
[[MyModel queue] addOperation:operation]; // operation is a NSInvocationOperation that calls the delegate parse method
}];

我在入口点的所有 NSInvocationOperation Invoked 方法中添加了这段代码:

if([NSThread isMainThread]){
NSLog(@"****************************Running <operation x> on Main thread");
}

每次 UI 卡住时都会打印该行。这表明整个操作是在主线程上运行的。它实际上始终是在主线程上运行的 hugeDataProcessorOp。我认为这是因为操作总是最后从服务器收到它的答复。

最佳答案

在对我自己的代码进行大量调查后,我可以确认这是一个编码错误。

还有一个旧的调用没有通过 NSInvocationOperation,而是调用了 NSInvocationOperation 应该直接调用的选择器(因此没有使用并发的 NSOperationQueue

这意味着 NSOperationQueue 不使用主线程(除非它是由 +mainQueue 检索的)。

关于iphone - 如何防止IOS并发NSOperation在主线程上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585575/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com