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:
- Void TTNetworkRequestStarted (); // starts the Status column flywheel.
- Void TTNetworkRequestStopped (); // stop the Status column Flywheel
- # Define TTIMAGE (_ URL) [[TTURLCache sharedCache] imageForURL: _ URL] // extract the image from the buffer. If not, download the image online.
Cache Scheme
- typedef enum {
- TTURLRequestCachePolicyNone = 0,
- TTURLRequestCachePolicyMemory = 1,
- TTURLRequestCachePolicyDisk = 2,
- TTURLRequestCachePolicyNetwork = 4,
- TTURLRequestCachePolicyNoCache = 8,
- TTURLRequestCachePolicyEtag = 16 | TTURLRequestCachePolicyDisk,
- TTURLRequestCachePolicyLocal
- = (TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk),
- TTURLRequestCachePolicyDefault
- = (TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk
- | TTURLRequestCachePolicyNetwork),
- } TTURLRequestCachePolicy;
A main class of network connection: TTURLRequest. equivalent to NSUrlRequest of ios
- # Import <Foundation/Foundation. h>
-
- @ Interface TTURLRequest: NSObject
-
- @ Property (nonatomic, copy) NSString * urlPath; // Url of the network request
-
- @ Property (nonatomic, copy) NSString * URL _ TTDEPRECATED_METHOD; // It is the same as the one above. It's really troublesome for foreigners.
-
- @ Property (nonatomic, copy) NSString * httpMethod; // http Request Method
-
- @ Property (nonatomic, retain) id <TTURLResponse> response; // This is the object that receives data.
-
- @ Property (nonatomic, retain) NSData * httpBody; // This is postBody
-
- @ Property (nonatomic, copy) NSString * contentType; if it is post or put, this object will be filled. The default value is multipart/form-data.
-
- @ Property (nonatomic, readonly) NSMutableDictionary * parameters; // The parameters of POST/PUT are key and value.
-
- @ Property (nonatomic, readonly) NSMutableDictionary * headers; // http Request Header
-
- @ Property (nonatomic) TTURLRequestCachePolicy cachePolicy; // The default value is TTURLRequestCachePolicyDefault, which has been mentioned above.
-
- @ 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.
-
- @ Property (nonatomic, copy) NSString * cacheKey; // This is the key Etag of the http cache flag Etag.
-
- @ Property (nonatomic, retain) id userInfo;
-
- @ Property (nonatomic, retain) NSDate * timestamp;
-
- @ Property (nonatomic) BOOL isLoading; // whether the network connection is in progress
-
- @ 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
-
- @ Property (nonatomic) NSInteger totalBytesLoaded; // number of bytes loaded in this request
-
- @ Property (nonatomic) NSInteger totalBytesExpected;
-
- @ Property (nonatomic) NSInteger totalBytesDownloaded; // number of bytes of the current file on the slave server
-
- @ Property (nonatomic) NSInteger totalContentLength; // The length of the request content, that is, the number of words
-
- @ Property (nonatomic) BOOL respondedFromCache; // whether the resources requested by this network come from the cache
-
- @ Property (nonatomic, assign) BOOL filterPasswordLogging;
-
- @ Property (nonatomic) NSStringEncoding charsetForMultipart; // data encoding method when a request is sent using multipart/form-data
-
- @ 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.
-
-
- + (TTURLRequest *) request; // class method. you should understand this.
-
- + (TTURLRequest *) requestWithURL :( NSString *) URL delegate :( id/* <TTURLRequestDelegate> */) delegate; // same as above
-
- -(Id) initWithURL :( NSString *) URL delegate :( id/* <TTURLRequestDelegate> */) delegate; // same as above
-
- -(Void) setValue :( NSString *) value forHTTPHeaderField :( NSString *) field; // you can specify the request header.
-
- -(Void) addFile :( NSData *) data mimeType :( NSString *) mimeType fileName :( NSString *) fileName; // send a binary file
-
- -(BOOL) send; // sends an asynchronous request. If yes is returned, the requested resource comes from the cache.
-
- -(BOOL) sendSynchronously; // send the same request
-
- -(Void) cancel; // you can log out of the network.
-
- -(NSURLRequest *) createNSURLRequest; // same as above
-
- @ End
-
- According to the directory in the three20 TTNetWorkDemo project, the next directory should be Responses
- In this folder, there are two classes, TTURLDataResponse TTURLImageResponse, which are actually very simple and implement a protocol method.
- These two classes are prepared for the TTURLRequest attribute responses mentioned above.
-
- Next, the requests folder contains many files and folders.
-
- 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.
- You can take a look. It doesn't make much sense if I write it again.
-
- Next, the TTURLRequestCachePolicy. h file defines a struct, which is prepared for the cacheKey of the TTUrlRequest.
- In my blog posts.
-
- Updating
-
- 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!
-
- @ Interface TTURLRequestQueue: NSObject
-
- @ Property (nonatomic) BOOL suincluded; // whether to accept the new network connection. If no, the new network connection request is suspended until it is yes.
- Because network connections reduce the performance of mobile phones, three20 provides an excuse for control.
-
- @ 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.
- In fact, we have a better way to prevent such incidents. Send me an email.
-
- @ 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
- 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.
-
- @ 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!
-
- + (TTURLRequestQueue *) mainQueue; // obtain the shared TTURLRequestQueue. TTUrlRequest will be added here.
-
- + (Void) setMainQueue :( TTURLRequestQueue *) queue; set the shared TTURLRequestQueue. This is usually not used.
-
- -(BOOL) sendRequest :( TTURLRequest *) request; // sends an asynchronous request. If yes is returned, the resource is obtained from the cache. Vice versa.
-
- -(BOOL) sendSynchronousRequest :( TTURLRequest *) request; // send a synchronous request. The return value is the same as the preceding one.
-
- -(Void) cancelRequest :( TTURLRequest *) request; // cut a network connection
-
- -(Void) cancelRequestsWithDelegate :( id) delegate; // you can discuss the use of this excuse.
-
- -(Void) cancelAllRequests; // stop all network links, including the suspended network links.
-
- -(NSURLRequest *) createNSURLRequest :( TTURLRequest *) request URL :( NSURL *) URL; // create a network request, which is generally called by itself. We cannot reach
-
- @ 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
- @ Interface TTRequestLoader: NSObject
-
- @ Property (nonatomic, readonly) NSArray * requests; // The TTUrlRequests attached to the loader
-
- @ Property (nonatomic, readonly) NSString * urlPath; // request url
-
- @ Property (nonatomic, readonly) NSString * cacheKey; // The requested cacheKey, used to indicate the data cache
-
- @ Property (nonatomic, readonly) TTURLRequestCachePolicy cachePolicy; // cache plan
-
- @ Property (nonatomic, readonly) NSTimeInterval cacheExpirationAge; // The cache resource validity period.
-
- @ Property (nonatomic, readonly) BOOL isLoading; // is the network connection in progress?
-
- @ Property (nonatomic, readonly) NSString * URL _ TTDEPRECATED_METHOD; // url
-
- -(Id) initForRequest :( TTURLRequest *) request queue :( TTURLRequestQueue *) queue;
-
- -(Void) addRequest :( TTURLRequest *) request; // no more
- -(Void) removeRequest :( TTURLRequest *) request; // no more
-
- -(BOOL) cancel :( TTURLRequest *) request; // releases the network link
-
- -(NSError *) processResponse :( NSHTTPURLResponse *) response data :( id) data;
- -(Void) dispatchError :( NSError *) error;
- -(Void) dispatchLoaded :( NSDate *) timestamp;
- -(Void) dispatchAuthenticationChallenge :( NSURLAuthenticationChallenge *) challenge;
- -(Void) cancel;
Summary:IPhone DevelopmentStudy NotesThree20 NetWorkThe tutorial is complete. I hope this article will help you!