Over the past couple of weeks, I was profiling MKNetworkKit and looking for bottlenecks.
I found around five major bottlenecks.
Performance Fix 1:
Decoding images in Background
UIImage will not decompress the PNG/JPG image until it’s actually displayed. This is fine for artwork or pattern images that you normally bundle with your iOS app. But for displaying avatar images on a Twitter timeline or a photo library application, the performance could be improved drastically by decompressing the image in the background. Thanks to Krzysztof Zabłocki (@merowing_) for the code snippet.
You can now use the method, imageAtUrl:size:completionHandler:errorHandler: to get a decompressed image from a MKNetworkEngine.
Performance Fix 2:
Decoding JSONs in a background thread
I added a method responseJSONWithCompletionHandler: to MKNetworkOperation to decode JSON in the background.
From here on, responseJSONWithCompletionHandler: is the recommended way to decode JSON instead of the older method, responseJSON.
Performance Fix 3:
Caching in a background thread
Caching is now thread safe and runs in a serial GCD queue completely in background without blocking the UI. you wouldn’t even know that the requests are being cached!
Performance Fix 4:
Replaced NSDateFormatter with strptime_l
MKNetworkOperation used NSDateFormatter to parse the dates in the Expires and Cache-Control header. While I did use a dispatch_once to mitigate the issue, dispatch_once did not eliminate the problem completely. The right way to do date formatting when the date format is fixed and unlocalized, is to use strptime_l I was dabbling around with strptime_l today and got this pull request from Bo98. He did an awesome job with strptime_l, and improved the overall performance.
Performance Fix 5:
There is one more performance tune up that could speed up the enqueueOperation. I’m looking at it now. You should see another post in a couple of days.
Overall, the latest on Github should offer a lot more performance. I hope you liked it. Keep the pull requests coming!
Follow me on Twitter