About mknetworking Own maintenance

Source: Internet
Author: User

About mknetworking Own maintenance 

Personal comparisons tend to be mknetworking, because in small projects this network request framework can be said to be lightweight. But there is a part of the function missing or functional surplus, and need to optimize the place. So decided to maintain a MK repo for his own use.

In github:mknetworking-dlprotecting here you can see the source code and demo project.

0.INIT

    • Create Engin

      NSDictionary * customHeader = @{                                  @"version" : @"0.0.1",                                  @"channel" : @"appleStore" }; MKNetworkEngine * engin = [[MKNetworkEngine alloc] initWithHostName:@"api.devdylan.com" apiPath:@"MKNetFrame" customHeaderFields:customHeader]; // 使用缓存 [engin useCache];
    • Create operation and add callbacks
    • Perform

1.GET

    -Get/download-//Use Operationwithpath: Create operation, will automatically stitching Engin host and Apipath, etc.mknetworkoperation * operation = [Engin Operationwithpath:@ "Student.json" params:Nil HttpMethod:@ "GET" SSL:NO];Use operationwithurlstring: Create operation, do not stitch Engin host and ApipathMknetworkoperation * Operation_url = [Engin operationwithurlstring:@ "Http://api.devdylan.com/MKNetFrame/Student.json" params:Nil HttpMethod:@ "GET"];NSLog (@ "%@", Operation. URL);NSLog (@ "%@", Operation_url. URL); [Operation Addcompletionhandler:^ (mknetworkoperation *completedoperation) { //Here the request is processed at the right time NSLog (@ "%@", Completedoperation. Responsejson); if ([completedoperation Iscachedresponse]) { //What you get now is the data in the cache} else { //You are now getting data from the server}} errorhandler:^ (mknetworkoperation *completedoperation, nserror *error) { //This returns server-level errors or JSON parsing errors. NSLog (@ "%@", completedoperation. Error);}]; //execute current network request [Engin Enqueueoperation:operation];            

2.POST

    -Post/upload-//Mknetworkoperation * Operation_post = [Engin Operationwithpath:@ "Student.json" Params:nil HttpMethod: @ "POST" Ssl:no]; [Operation_post addcompletionhandler:^ (mknetworkoperation *completedOperation) {//here to handle the request right when nslog (@ "%@", Completedoperation.responsejson); } errorhandler:^ (mknetworkoperation *completedoperation,  Nserror *error) {//This returns server-level errors or JSON parsing errors. nslog (@ "%@", completedoperation .error); }]; [Engin enqueueoperation:operation_post forcereload:yes]; //set unread cache             

3.UPLOAD

    // 添加路径下文件    [operation_post addFile:@"FILE PATH" forKey:@"image.png"];    // 添加Data    [operation_post addData:[NSData data] forKey:@"image2.png"]; [operation_post onUploadProgressChanged:^(double progress) { // 上传进度 }];

4.DOWNLOAD

    // 下载文件时, 添加下载流    [operation addDownloadStream:[NSOutputStream outputStreamToFileAtPath:@"downloadPath/image.png" append:YES]];    [operation onDownloadProgressChanged:^(double progress) { // 下载进度 }];

5.FORCELOAD

 [engin enqueueOperation:operation_post forceReload:YES]; // 设置不读缓存

6.FREEZABLE

// 设置当前operation冻结    [operation_post setFreezable:YES];    // 解除冻结    [operation_post setFreezable:NO];

7.OTHERS

    [Operation_post onnotmodified:^{//call at Server 304}]; Cancel Request[Operation_post Canceloperation]; Request only in WiFi case, default is no Engin.wifionlymode = YES[engin Cancelalloperation][ Engin removeoperation:operation_post][engin emptycache]; [engin setreachabilitychangedhandler:^  (networkstatus statu) {//Handle the change of network status}] [operation_post Setoperationstatechangedhandler:^ (mknetworkoperationstate state) {//When operation status changes, back to here}]               

 

About mknetworking Own maintenance

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.