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.Framework, SystemConfiguration.framework, Security.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.

    if (object == _sharedNetworkQueue && [keyPath isEqualToString:@"operationCount"]) {
 
        [UIApplication sharedApplication].networkActivityIndicatorVisible =
        ([_sharedNetworkQueue.operations count] > 0);
    }

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,

[[MKNetworkEngine sharedEngine] useCache];

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.





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章