Git Product home page Git Product logo

iasyncurlsession's Introduction

iAsyncUrlSession

Adapters to make iOS7 networking classes work as async operations in term sof iAsync library

The NSURLDownloadTask is wrapped into a class which is similar to NSURLConnection and uses block callbacks to notify the user. It allows handling session events independently from other downloads. See the details below.

License : BSD

The library project has two static library targets. One of them contains the JNUrlSessionConnection class that depends only on Foundation.framework. The other target contains the mentioned connection class and wrappers for integration with iAsync.

Rationale

iOS7 has brought the new NSURLSession API that offers a completely different approach of networking.

On the other hand, it introduces a challenge for those who got used to handling downloads separately. NSURLSession uses a single delegate object for all tasks leaving the work of events dispatching to developers. One can solve this task by applying any approach listed below :

1). Notifications

2). Emulate task delegates using a mutex ( @synchronized() block ).

Note: This approach is used in AFNetworking 2.0

3). Create a dedicated instance of NSURLSession for each networking task

####This library uses approach [3]

#Features

  1. Block based callbacks
  2. Lock-free
  3. Integration with iAsync

#Limitations

  1. Only NSURLSessionDownloadTask is supported. Saving
  2. Suspend and resume operations are not supported
  3. Background downloads are not supported

#Using JNUrlSessionConnection Class

This class is as simple as NSURLConnection. All you need is :

  • Session configuration object
  • HTTP request
  • Subscribe to callbacks
    NSURLSessionConfiguration* sessionConfig = [ NSURLSessionConfiguration defaultSessionConfiguration ];

    NSURL* url = [ NSURL URLWithString: @"https://raw.github.com/iAsync/iAsyncUrlSession/master/README.md" ];
    NSURLRequest* request = [ NSURLRequest requestWithURL: url ];

    JNUrlSessionConnectionCallbacks* callbacks = [ JNUrlSessionConnectionCallbacks new ];
    callbacks.completionBlock = ^void( NSURL* temporaryFileUrl, NSError* blockError )
    {
        // Do work with downloaded data
    };
    
    
    JNUrlSessionConnection* connection =
    [ [ JNUrlSessionConnection alloc ] initWithSessionConfiguration: sessionConfig
                                               sessionCallbackQueue: [ NSOperationQueue mainQueue ]
                                                        httpRequest: request
                                                          callbacks: callbacks ];
    connection.shouldCopyTmpFileToCaches = YES;
    [ connection start ];

#Using JNUrlSessionConnection as an Asynchronous Operation

In order to avoid the callback hell problem the integration with iAsync library has been implemented. Moreover, this integration makes the code look easier.

    NSURLSessionConfiguration* sessionConfig = [ NSURLSessionConfiguration defaultSessionConfiguration ];

    NSURL* url = [ NSURL URLWithString: @"https://raw.github.com/iAsync/iAsyncUrlSession/master/README.md" ];
    NSURLRequest* request = [ NSURLRequest requestWithURL: url ];
    
    
    
    JFFAsyncOperation* asyncDownload = 
    [ JNUrlSessionOperationBuilder  asyncTempFileDownloadWithRequest: request
                                                           authBlock: nil 
                                                       sessionConfig: sessionConfig
                                             urlSessionCallbackQueue: [ NSOperationQueue mainQueue ] ];
    
    
    JFFAsyncOperationProgressHandler progressCallbackBlock = nil;
    JFFCancelAsyncOperationHandler cancelCallbackBlock = nil;
    loader( progressCallbackBlock, cancelCallbackBlock, ^void( NSURL* temporaryFileUrl, NSError* blockError )
    {
         // Do work with downloaded data
    });

iasyncurlsession's People

Contributors

dodikk avatar igor-khomich avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.