Three20 NetWork tutorial

Source: Internet
Author: User

IPhone DevelopmentStudy NotesThree20 NetWorkThe tutorial is the content to be introduced in this article, mainly to learnThree20 NetWork. This article provides a detailed description of the details.

I prefer to paste Code as follows:

 
 
  1. Void TTNetworkRequestStarted (); // starts the Status column flywheel.
  2. Void TTNetworkRequestStopped (); // stop the Status column Flywheel
  3. # Define TTIMAGE (_ URL) [[TTURLCache sharedCache] imageForURL: _ URL] // extract the image from the buffer. If not, download the image online.

Cache Scheme

 
 
  1. typedef enum {  
  2.   TTURLRequestCachePolicyNone    = 0,  
  3.   TTURLRequestCachePolicyMemory  = 1,  
  4.   TTURLRequestCachePolicyDisk    = 2,  
  5.   TTURLRequestCachePolicyNetwork = 4,  
  6.   TTURLRequestCachePolicyNoCache = 8,  
  7.   TTURLRequestCachePolicyEtag    = 16 | TTURLRequestCachePolicyDisk,  
  8.   TTURLRequestCachePolicyLocal 
  9.   = (TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk),  
  10.   TTURLRequestCachePolicyDefault 
  11.   = (TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk  
  12.      | TTURLRequestCachePolicyNetwork),  
  13. } TTURLRequestCachePolicy; 

A main class of network connection: TTURLRequest. equivalent to NSUrlRequest of ios

 
 
  1. # Import <Foundation/Foundation. h>
  2.  
  3. @ Interface TTURLRequest: NSObject
  4.  
  5. @ Property (nonatomic, copy) NSString * urlPath; // Url of the network request
  6.  
  7. @ Property (nonatomic, copy) NSString * URL _ TTDEPRECATED_METHOD; // It is the same as the one above. It's really troublesome for foreigners.
  8.  
  9. @ Property (nonatomic, copy) NSString * httpMethod; // http Request Method
  10.  
  11. @ Property (nonatomic, retain) id <TTURLResponse> response; // This is the object that receives data.
  12.  
  13. @ Property (nonatomic, retain) NSData * httpBody; // This is postBody
  14.  
  15. @ Property (nonatomic, copy) NSString * contentType; if it is post or put, this object will be filled. The default value is multipart/form-data.
  16.  
  17. @ Property (nonatomic, readonly) NSMutableDictionary * parameters; // The parameters of POST/PUT are key and value.
  18.  
  19. @ Property (nonatomic, readonly) NSMutableDictionary * headers; // http Request Header
  20.  
  21. @ Property (nonatomic) TTURLRequestCachePolicy cachePolicy; // The default value is TTURLRequestCachePolicyDefault, which has been mentioned above.
  22.  
  23. @ Property (nonatomic) NSTimeInterval cacheExpirationAge; // The cache extraction time is one week by default. That is to say, the cache will be downloaded again after more than a week.
  24.  
  25. @ Property (nonatomic, copy) NSString * cacheKey; // This is the key Etag of the http cache flag Etag.
  26.  
  27. @ Property (nonatomic, retain) id userInfo;
  28.  
  29. @ Property (nonatomic, retain) NSDate * timestamp;
  30.  
  31. @ Property (nonatomic) BOOL isLoading; // whether the network connection is in progress
  32.  
  33. @ Property (nonatomic) BOOL shouldHandleCookies; // here is the coockie settings. The default value is yes. It is to store and send coockies. No is the opposite
  34.  
  35. @ Property (nonatomic) NSInteger totalBytesLoaded; // number of bytes loaded in this request
  36.  
  37. @ Property (nonatomic) NSInteger totalBytesExpected;
  38.  
  39. @ Property (nonatomic) NSInteger totalBytesDownloaded; // number of bytes of the current file on the slave server
  40.  
  41. @ Property (nonatomic) NSInteger totalContentLength; // The length of the request content, that is, the number of words
  42.  
  43. @ Property (nonatomic) BOOL respondedFromCache; // whether the resources requested by this network come from the cache
  44.  
  45. @ Property (nonatomic, assign) BOOL filterPasswordLogging;
  46.  
  47. @ Property (nonatomic) NSStringEncoding charsetForMultipart; // data encoding method when a request is sent using multipart/form-data
  48.  
  49. @ Property (nonatomic, readonly) NSMutableArray * delegates; // The proxy class for network requests. For details, let's take a look at it. It's very simple. It's just a proxy method.
  50.  
  51.  
  52. + (TTURLRequest *) request; // class method. you should understand this.
  53.  
  54. + (TTURLRequest *) requestWithURL :( NSString *) URL delegate :( id/* <TTURLRequestDelegate> */) delegate; // same as above
  55.  
  56. -(Id) initWithURL :( NSString *) URL delegate :( id/* <TTURLRequestDelegate> */) delegate; // same as above
  57.  
  58. -(Void) setValue :( NSString *) value forHTTPHeaderField :( NSString *) field; // you can specify the request header.
  59.  
  60. -(Void) addFile :( NSData *) data mimeType :( NSString *) mimeType fileName :( NSString *) fileName; // send a binary file
  61.  
  62. -(BOOL) send; // sends an asynchronous request. If yes is returned, the requested resource comes from the cache.
  63.  
  64. -(BOOL) sendSynchronously; // send the same request
  65.  
  66. -(Void) cancel; // you can log out of the network.
  67.  
  68. -(NSURLRequest *) createNSURLRequest; // same as above
  69.  
  70. @ End
  71.  
  72. According to the directory in the three20 TTNetWorkDemo project, the next directory should be Responses
  73. In this folder, there are two classes, TTURLDataResponse TTURLImageResponse, which are actually very simple and implement a protocol method.
  74. These two classes are prepared for the TTURLRequest attribute responses mentioned above.
  75.  
  76. Next, the requests folder contains many files and folders.
  77.  
  78. TTURLRequestDelegate. h: This file defines a protocol. This Protocol is similar to the NSUrlRequest delegate in ios, and the method and method name are very similar.
  79. You can take a look. It doesn't make much sense if I write it again.
  80.  
  81. Next, the TTURLRequestCachePolicy. h file defines a struct, which is prepared for the cacheKey of the TTUrlRequest.
  82. In my blog posts.
  83.  
  84. Updating
  85.  
  86. Next is a very important class. It is important to say that it plays the role of the thread pool or thread queue in ios. In other words, this class is the manager of all network links in the app. Amazing!
  87.  
  88. @ Interface TTURLRequestQueue: NSObject
  89.  
  90. @ Property (nonatomic) BOOL suincluded; // whether to accept the new network connection. If no, the new network connection request is suspended until it is yes.
  91. Because network connections reduce the performance of mobile phones, three20 provides an excuse for control.
  92.  
  93. @ Property (nonatomic) NSUInteger maxContentLength; // The maximum length of data downloaded from the network. The default value is 150000 bytes. This is to prevent excessive memory usage.
  94. In fact, we have a better way to prevent such incidents. Send me an email.
  95.  
  96. @ Property (nonatomic, copy) NSString * userAgent; // sets the http request's user-Agent header. It is a special string header that allows the server to identify the user's
  97. Operating System and version, CPU type, browser and version, browser rendering engine, browser language, and browser plug-in. If this parameter is set, all requests are used.
  98.  
  99. @ Property (nonatomic) CGFloat imageCompressionQuality; // coefficient of the compressed image. The default value is 0. 75. This compression affects resolution. Exercise caution when setting this item!
  100.  
  101. + (TTURLRequestQueue *) mainQueue; // obtain the shared TTURLRequestQueue. TTUrlRequest will be added here.
  102.  
  103. + (Void) setMainQueue :( TTURLRequestQueue *) queue; set the shared TTURLRequestQueue. This is usually not used.
  104.  
  105. -(BOOL) sendRequest :( TTURLRequest *) request; // sends an asynchronous request. If yes is returned, the resource is obtained from the cache. Vice versa.
  106.  
  107. -(BOOL) sendSynchronousRequest :( TTURLRequest *) request; // send a synchronous request. The return value is the same as the preceding one.
  108.  
  109. -(Void) cancelRequest :( TTURLRequest *) request; // cut a network connection
  110.  
  111. -(Void) cancelRequestsWithDelegate :( id) delegate; // you can discuss the use of this excuse.
  112.  
  113. -(Void) cancelAllRequests; // stop all network links, including the suspended network links.
  114.  
  115. -(NSURLRequest *) createNSURLRequest :( TTURLRequest *) request URL :( NSURL *) URL; // create a network request, which is generally called by itself. We cannot reach
  116.  
  117. @ End

Okay, this class has been explained. Actually, I am writing it here. Why didn't I access all the network links in the app TTUrlRequest? In fact, this interface is put into the _ request attribute in TTRequestLoader. h. Next we will explain TTRequestLoader. h

TTRequestLoader. H manages the TTUrlRequest settings and uses ios's NSURLConnection for actual network connections. And manage the TTUrlRequest callback function. For example, you can start to download which function to call and end with which function to call.

TTRequestLoader implements the NSURLConnectionDelegate protocol.

In fact, each TTUrlRequest has its own TTRequestLoader.

TTRequestLoader is installed in TTUrlRequest with the same settings

 
 
  1. @ Interface TTRequestLoader: NSObject
  2.  
  3. @ Property (nonatomic, readonly) NSArray * requests; // The TTUrlRequests attached to the loader
  4.  
  5. @ Property (nonatomic, readonly) NSString * urlPath; // request url
  6.  
  7. @ Property (nonatomic, readonly) NSString * cacheKey; // The requested cacheKey, used to indicate the data cache
  8.  
  9. @ Property (nonatomic, readonly) TTURLRequestCachePolicy cachePolicy; // cache plan
  10.  
  11. @ Property (nonatomic, readonly) NSTimeInterval cacheExpirationAge; // The cache resource validity period.
  12.  
  13. @ Property (nonatomic, readonly) BOOL isLoading; // is the network connection in progress?
  14.  
  15. @ Property (nonatomic, readonly) NSString * URL _ TTDEPRECATED_METHOD; // url
  16.  
  17. -(Id) initForRequest :( TTURLRequest *) request queue :( TTURLRequestQueue *) queue;
  18.  
  19. -(Void) addRequest :( TTURLRequest *) request; // no more
  20. -(Void) removeRequest :( TTURLRequest *) request; // no more
  21.  
  22. -(BOOL) cancel :( TTURLRequest *) request; // releases the network link
  23.  
  24. -(NSError *) processResponse :( NSHTTPURLResponse *) response data :( id) data;
  25. -(Void) dispatchError :( NSError *) error;
  26. -(Void) dispatchLoaded :( NSDate *) timestamp;
  27. -(Void) dispatchAuthenticationChallenge :( NSURLAuthenticationChallenge *) challenge;
  28. -(Void) cancel;

Summary:IPhone DevelopmentStudy NotesThree20 NetWorkThe tutorial is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.