Search code examples
iphoneiosrestkitafnetworkingnsoperationqueue

UIImageView+AFNetworking image request queue blocks other network requests from RestKit


I am downloading Data with RestKit by creating RKObjectRequestOperations and adding them to RestKit's queue:

RKObjectRequestOperation *operation = [RK objectRequestOperationWithRequest:request
       success:...
       failure:...];
[RK enqueueObjectRequestOperation:operation];

That one works well. Additionally, this data is displayed in a list view that contains UIImageViews displaying related user icons. These user icons, however, are not downloaded via RestKit, but via the underlying AFNetworking library. UIImageView+AFNetworking does the job also pretty well (including the caching):

[self setImageWithURLRequest:userIconRequest
            placeholderImage:anonymousUser
                     success:nil
                     failure:...];

The problem is that those 15 user icon requests from the image views block the processing of the RestKit request that should be loading the next page immediately. I can see the list displaying the "loading" row as well as the first user icons. In the moment the last image finished loading the next page appends itself.

Looking into the implementation of UIImageView+AFNetworking shows that it is using an own NSOperation queue instance that is serializing requests. However that should not interfere with RestKit I suppose.

Also adding NSOperationQueuePrioritys to all requests does not change a thing. Maybe internally, network requests are serialized in a different way? How can I prioritize those requests?

Thanks in advance.


Solution

  • NSURLConnection has an undocumented maximum number of connections.

    Additionally, UIImageView+AFNetworking's operation queue has NSOperationQueueDefaultMaxConcurrentOperationCount maximum current requests, which is likely a bad choice for your use case according to this convincing-sounding AFNetworking discussion.

    You need to throttle. I see two simple solutions:

    1. Modify UIImageView+AFNetworking to have, say, 4 maximum concurrent operations.
    2. Modify UIImageView+AFNetworking to use the same operation queue as RestKit, in which case the priority you set will matter.