Question

I read something about multi thread access on DB but still not sure best way to do that for reading/writing in conjunction with async network download. For instance I will have a page with images from the web, so I'm retrieving them by their URLs, using AFNetworking but I want to check first on my DB and write on it (or disk) the retrieved image for future use. What can be the best way to do that without blocking the UI (like when scrolling)? If I do that with a singleton that reads/writes it block the main thread. Thanks for any tips on that.

Was it helpful?

Solution

AFNetworking is not the tool for that. Instead, you can take advantage of the built-in functionality of NSURLCache--specifically Peter Steinberger's fork of SDURLCache.

In -applicationDidFinishLaunchingWithOptions:, do NSURLCache +setSharedCache: with an instance of SDURLCache (with some amount of disk space allocated). All requests made through UIWebView (and AFNetworking, for that matter) will automatically be routed through NSURLCache before the request is made to check the cache. It's unobtrusive, drop-in, and follows cache directives correctly, and it should solve your problem quite nicely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top