UIImageView+AFNetworking setImageWithURLRequest us

2019-08-12 06:09发布

问题:

I am using RestKit (0.20.0) for my Rest Api implementation - all the GETs, POSTs etc. The GETs return some urls for the images. I am using UIImageView+AFNetworking setImageWithURLRequest function to download these. I was surprised to see RestKit logs getting printed for these fetches so I did some debugging. It seems that some notification is sent by AFNetworking that eventually lands on RestKit.

Looks like its happening because AFNetworkingOperationDidStartNotification is fired for the fetch of the image and RK has registered for this notification in RKObjectRequestOperation.m.

        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(HTTPOperationDidStart:)
                                                 name:AFNetworkingOperationDidStartNotification
                                               object:nil];

My questions are -

  1. Should it matter to me who is routing this given that my thinking behind using this api was to ensure that the image fetches are in completely separate/exclusive threads than RK.

  2. Does this impact my CoreData implementation? Will this image get cached - I am guessing it wont be because I am not explicitly storing it but can it possibly impact performance if these operations start to happen on the same pool as the Rest api operations?

  3. Is this the right implementation? Should I cache the images as well in CoreData and not use AFNetworking api at all?

  4. Slightly off but related topic - I plan on getting hundreds of items from the server, each of these items can have many items as well. and each of these will have images as well. So its a pretty network heavy application. Are there any best practices to manage throttling of these requests to avoid performance hiccups?

Thx in advance for the help..

回答1:

Should it matter to me who is routing this given that my thinking behind using this api was to ensure that the image fetches are in completely separate/exclusive threads than RK.

No, it shouldn't. The request will still run on a different thread.

Does this impact my CoreData implementation? Will this image get cached - I am guessing it wont be because I am not explicitly storing it but can it possibly impact performance if these operations start to happen on the same pool as the Rest api operations?

The image will only be cached in memory (if memory is available). Generally I agree with @m-farhan that SDWebImage would be a better choice, not to separate from Core Data but to cache the images to disk so you don't keep downloading them. Generally you don't want to store the images in Core Data.

Any requests can impact performance if you make them all at the same time. You should limit your concurrent network operations to ~ 4 or 5.

Is this the right implementation? Should I cache the images as well in CoreData and not use AFNetworking api at all?

See above.

Slightly off but related topic - I plan on getting hundreds of items from the server, each of these items can have many items as well. and each of these will have images as well. So its a pretty network heavy application. Are there any best practices to manage throttling of these requests to avoid performance hiccups?

See above. Set the concurrent count on the operationQueue of the RKObjectManager.



回答2:

I don't have alot of RestKit knowledge but i worked with alot of API's I just AFNetworking with JSON parser, and process it in NSDictionery. how ever for image cache problem best solution according to me is SDWebImages library. Its simple and work alone and it will take care of all cache problems. and your question 4. Use AFNetworking requestManager and dont process your data on main thread (I think RestKit and/or AFNetworking) will take care of this and to implement SDWebImages is simple as this

#import "SDWebImage/UIImageView+WebCache.h"
[userImage setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/12345/picture?type=large"]]
              placeholderImage:[UIImage imageNamed:@"unknownUser.png"]];