0

AFNetworking default runLoopModes is NSRunLoopCommonModes.I want use NSDefaultRunLoopMode,and set it

 operation.runLoopModes = [NSSet setWithObject:NSDefaultRunLoopMode];

But it doesn't work. when I scroll the scrollView,download task still running. Anybody can help me? thanks.

Leestar
  • 43
  • 6
  • I have never used AFNetworking and I guess that this operation running in another thread, not in main thread – tikhop Aug 31 '12 at 10:30
  • so you want to pause network operations in tracking runloop mode, but why? – Felix Aug 31 '12 at 11:29
  • yes,I want to pause network operations,when scrolling the scrollview.because network may effect scrollview's response. – Leestar Sep 03 '12 at 02:35

2 Answers2

1

So here we go, alternative solution, without hardcoded injection. Assuming you are using 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];
}

Also check out Foursquare's solution, which relies on the same technique.

Roman B.
  • 3,598
  • 1
  • 25
  • 21
0

Recently I encountered similar question, while I was implementing images lazy loading in UITableView. User scrolling should have higher priority here.

Your direction is correct, the problem is in runloop modes. With UIImageView+AFNetworking extension, images are loaded with AFURLConnectionOperation, which is working on a separate network thread (see implementation), so NSURLConnections are running not on main thread.

To make the tasks stop when UITrackingRunLoopMode is active, you need to move networking code on main thread somehow.

Possible solutions:

  1. Setup default runloop mode for your request operation.

    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
    [requestOperation setRunLoopModes:[NSSet setWithObject:NSDefaultRunLoopMode]];
    
  2. Put connection on main thread (that's ok, NSURLConnections are running their own threads): Find + (NSThread *)networkRequestThread and make it return [NSThread mainThread]. That might be not suitable with other situations, beware locks.

I am still not sure why AFNetworking is creating it's separate network thread. To process incoming data in background? If anybody have a guess, please reply.

Also see this question.

Community
  • 1
  • 1
Roman B.
  • 3,598
  • 1
  • 25
  • 21
  • Thank you for your answer. Is there a way to don't modify AFNetWoring's code? – Leestar Sep 03 '12 at 02:41
  • I think there is. I just have looked over Facebook's Three20 sources and found this: they override NSOperationQueue 'suspend' property. All it does is "determines if new load requests are allowed to reach the network". On scrollViewWillBeginDragging you do [operationQueue setSuspended:YES], on scrollViewDidEndDragging you resume [operationQueue setSuspended:NO]. – Roman B. Sep 03 '12 at 12:16