MKNetworkKit 详细介绍

进行网络请求时,ASIHTTPRequest和AFNetworking,大家都比较熟悉了;
今天跟大家学习新的网络框架:MKNetworkKit


MKNetworkKit 简介

MKNetworkKit 采用的纯OBJECT-C编写一个网络请求框架; 基于block,ARC,非常简洁易用。
MKNetworkKit 结合了ASIHTTPRequest 和 AFNetworking优良特性,衍生出更多的易用的新特性,用最少最简单的代码实现网络请求;

  1. 为什么要使用MKNetworkKit?

  1. 整个APP框架中只开启了一个网络请求队列
  2. 自动调整请求队列大小和自动显示网络加载
  3. indicator (非常人性化)
  4. 高性能后台缓存请求数据(基于HTTP 1.1)
  5. 集成了图片缓存框架,无需额外的第三方图片缓存框架
  6. 支持后台图片解压
  7. 相同的一个网络请求只执行一次operation
  8. 支持本地提醒当后台操作失败
  9. 冻结当然网络操作请求,当网络中断的适合,当网络恢复适合又能继续上次操作,无需进行重新创建请求(very GOOD)

HOW TO USE?

Adding the MKNetworkKit

  1. Drag the MKNetworkKit directory to your project.  (拷贝进的项目当当中)
  2. Add the CFNetwork.FrameworkSystemConfiguration.frameworkSecurity.framework andImageIO.Framework. 4个依赖库
  3. Include MKNetworkKit.h to your PCH file (头文件中引进MKNetworkKit.h框架)
  4. Delete NSAlert+MKNetworkKitAdditions.h file if you are building for iOS.(不要与MKNetworkKit 提示冲突,MKNetworkKit会自动为你进行显示隐藏
  5. Delete UIAlertView+MKNetworkKitAdditions.h file if you are building for Mac.(同上

详情BLOG介绍:MKNetworkKit

下载地址:GIT:MKNetworkKit


MKNetworkKit特性介绍:


Super light-weight

The complete kit is just 2 major classes and some category methods. This means, adopting MKNetworkKit should be super easy.

Single Shared Queue for your entire application.

Apps that depend heavily on Internet connectivity should optimize the number of concurrent network operations. Unfortunately, there is no networking framework that does this correctly. Let me give you an example of what can go wrong if you don’t optimize/control the number of concurrent network operations in your app.

Let’s assume that you are uploading a bunch of photos (think Color or Batch) to your server. Most mobile networks (3G) don’t allow more than two concurrent HTTP connections from a given IP address. That is, from your device, you cannot open more than two concurrent HTTP connections on a 3G network. Edge is even worse. You can’t, in most cases, open more than one connection. This limit is considerably high (six) on a traditional home broadband (Wifi). However, since your iDevice is not always connected to Wifi, you should be prepared for throttled/restricted network connectivity. On any normal case, the iDevice is mostly connected to a 3G network, which means, you are restricted to upload only two photos in parallel. Now, it is not the slow upload speed that hurts. The real problem arises when you open a view that loads thumbnails of photos (say on a different view) while this uploading operations are running in the background. When you don’t properly control the queue size across the app, your thumbnail loading operations will just timeout which is not really the right way to do it. The right way to do this is to prioritize your thumbnail loading operation or wait till the upload is complete and the load thumbnails. This requires you to have a single queue across the entire app. MKNetworkKit ensures this automatically by using a single shared queue for every instance of it. While MKNetworkKit is not a singleton by itself, the shared queue is.

Showing the Network Activity Indicator correctly

While there are many third party classes that uses “incrementing” and “decrementing” the number of network calls and using that to show the network activity indicator, MKNetworkKit backs on the single shared queue principle and shows the activity indicator automatically when there is an operation running in the shared queue by observing (KVO) the operationCount property. As a developer, you normally don’t have to worry about setting the network activity indicator manually, ever again.

[objc] view plaincopy
  1. <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(166,19,144)">if</span> <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">(</span>object <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">==</span> _sharedNetworkQueue <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">&</span>amp;<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">&</span>amp; <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">[</span>keyPath isEqualToString<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">:</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(191,29,26)">@</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(191,29,26)">"operationCount"</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">]</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">)</span> <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">{</span>  
  2. p;  
  3.     <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">[</span>UIApplication sharedApplication<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">]</span>.networkActivityIndicatorVisible <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">=</span>  
  4.     <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">(</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">[</span>_sharedNetworkQueue.operations count<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">]</span> <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">&</span>gt; <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(36,0,217)">0</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">)</span>;  
  5. <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">}</span>  

Auto queue sizing

Continuing the previous discussion, I told that most mobile networks don’t allow more than two concurrent connections. So your queue size should be set to two, when the current network connectivity is 3G. MKNetworkKit automatically handles this for you. When the network drops to 3G/EDGE/GPRS, it changes the number of concurrent operations that can be performed to 2. This is automatically changed back to 6 when the device connects back to a Wifi network. With this technique in place, you will see a huge performance benefit when you are loading thumbnails (or multiple similar small requests) for a photo library from a remote server over 3G.

Auto caching

MKNetworkKit can automatically cache all your “GET” requests. When you make the same request again, MKNetworkKit calls your completion handler with the cached version of the response (if it’s available) almost immediately. It also makes a call to the remote server again. After the server data is fetched, your completion handler is called again with the new response data. This means, you don’t have to handle caching manually on your side. All you need to do is call one method,

[objc] view plaincopy
  1. <span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">[</span><span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">[</span>MKNetworkEngine sharedEngine<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">]</span> useCache<span style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; color:rgb(0,34,0)">]</span>;  

Optionally, you can override methods in your MKNetworkEngine subclass to customize your cache directory and in-memory cache cost.

Operation freezing

With MKNetworkKit, you have the ability to freeze your network operations. When you freeze an operation, in case of network connectivity losses, they will be serialized automatically and performed once the device comes back online. Think of “drafts” in your twitter client.

When you post a tweet, mark that network call as freezable and MKNetworkKit automatically takes care of freezing and restoring these requests for you! So the tweets get sent later without you writing a single line of additional code. You can use this for other operations like favoring a tweet or sharing a post from your Google reader client, adding a link to Instapaper and similar operations.

Performs exactly one operation for similar requests

When you load thumbnails (for a twitter stream), you might end up creating a new request for every avatar image. But in reality, you only need as many requests as there are unique URLs. With MKNetworkKit, every GET request you queue gets executed exactly once. MKNetworkKit is intelligent enough not to cache “POST” http requests.

Image Caching

MKNetworkKit can be seamlessly used for caching thumbnail images. By overriding a few methods, you can set how many images should be held in the in-memory cache and where in the Caches directory it should be saved. Overriding these methods are completely optional.

Performance

One word. SPEED. MKNetworkKit caching is seamless. It works like NSCache, except that, when there is a memory warning, the in-memory cache is written to the Caches directory.

Full support for Objective-C ARC

You normally choose a new networking framework for new projects. MKNetworkKit is not meant for replacing your existing framework (though you can, it’s quite a tedious job). On new projects, you will almost and always want to enable ARC and as on date of writing this, MKNetworkKit is probably the only networking framework that is fully ARC ready. ARC based memory management is usually an order of magnitude faster than non-ARC based memory management code.

你可能感兴趣的:(MKNetworkKit 详细介绍)