Objective c 是否设置运行模式?
AFNetworking默认的runLoopModes是NSRunLoopCommonModes。我想使用NSDefaultRunLoopMode,并设置它Objective c 是否设置运行模式?,objective-c,afnetworking,Objective C,Afnetworking,AFNetworking默认的runLoopModes是NSRunLoopCommonModes。我想使用NSDefaultRunLoopMode,并设置它 operation.runLoopModes = [NSSet setWithObject:NSDefaultRunLoopMode]; 但它不起作用。当我滚动scrollView时,下载任务仍在运行。 有人能帮我吗?谢谢。最近我在UITableView中实现图像延迟加载时遇到了类似的问题。在这里,用户滚动应该具有更高的优先级 您的方向
operation.runLoopModes = [NSSet setWithObject:NSDefaultRunLoopMode];
但它不起作用。当我滚动scrollView时,下载任务仍在运行。
有人能帮我吗?谢谢。最近我在UITableView中实现图像延迟加载时遇到了类似的问题。在这里,用户滚动应该具有更高的优先级 您的方向是正确的,问题在于运行循环模式。通过UIImageView+AFNetworking扩展,图像加载了AFURLConnectionOperation,它在单独的网络线程上工作(请参见实现),因此NSURLConnections不在主线程上运行 要在UITrackingRunLoopMode处于活动状态时停止任务,您需要以某种方式移动主线程上的网络代码 可能的解决办法:
AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setRunLoopModes:[NSSet setWithObject:NSDefaultRunLoopMode]];
+(NSThread*)networkRequestThread
,并使其返回[NSThread mainThread]
。这可能不适用于其他情况,请注意锁另请参见问题。我们开始了,替代解决方案,无需硬编码注入。假设您使用的是UIImageView+AFNetworking extension:
// user began scrolling
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[[UIImageView af_sharedImageRequestOperationQueue] setSuspended:YES];
}
// user released finger and scrolling animation is finished
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[[UIImageView af_sharedImageRequestOperationQueue] setSuspended:NO];
}
另外,请查看Foursquare's,它依赖于相同的技术。我从未使用过AFNetworking,我猜此操作在另一个线程中运行,而不是在主线程中运行。因此您希望以跟踪runloop模式暂停网络操作,但为什么?是的,我想暂停网络操作,滚动scrollview时。因为网络可能会影响scrollview的响应。谢谢您的回答。有没有办法不修改AFNetWoring的代码?我想有。我刚刚查看了Facebook的三个20来源,发现了这样一点:它们覆盖了NSOperationQueue的“suspend”属性。它所做的只是“确定是否允许新的加载请求到达网络”。在ScrollViewWillBeginDraging上执行[operationQueue setSuspended:YES],在ScrollViewDiEndDraging上继续[operationQueue setSuspended:NO]。