Latest 1.2.0
Homepage https://github.com/SoneeJohn/SJURLSessionOperation
License MIT
Platforms ios 7.0, osx 10.9, requires ARC
Dependencies AFNetworking
Authors

Version Status
Platform Status
License Status
Docs Status

SJURLSessionOperation creates and manages an NSURLSessionDownloadTask object based on a specified request and download location. SJURLSessionOperation is a subclass of NSOperation which then can be used with a NSOperationQueue. In addition, it uses AFURLSessionManager so, it requires AFNetworking.

Purpose of Class

As of Xcode 7, the NSURLConnection API has been officially deprecated by Apple. While the API will continue to function, no new features will be added, and Apple has advised all network based functionality to leverage NSURLSession going forward. The main purpose of this class is to make migration to the newer NSURLSession API easier. The class is intend for those that have apps that heavily relied on the NSOperation aspects of AFURLConnectionOperation.

Requirements

Usage

  1. Import SJURLSessionOperation:

    #import "SJURLSessionOperation.h"
  2. Instantiate SJURLSessionOperation

    
    NSURL *url = [NSURL URLWithString:@"https://www.example.com/bigfile1.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    SJURLSessionOperation *operation = [[SJURLSessionOperation alloc]initWithRequest:request targetLocation:[NSURL fileURLWithPath:@"~/Desktop/bigfile1.zip"]];
    

    Tip: You can use the resume data from a operation that previously failed. By setting the resume data the operation will start where the previous operation failed:

    SJURLSessionOperation *operation = [[SJURLSessionOperation alloc]initWithRequest:request targetLocation:[NSURL fileURLWithPath:@"~/Desktop/bigfile1.zip"] resumeData:failedOperation.operationResumeData];
  3. Start Operation

    [operation start];

    Operation Control

    //Pauses the execution of the operation.
    //Pausing a finished, cancelled, or paused operation has no effect.
    [operation pause];
    /**
    *  Resumes the execution of the paused operation.
    Resuming a operation that is not paused has no effect.
    */
    [operation resume];

    Setting CallBacks

    [operation setDownloadCompletionBlock:^(SJURLSessionOperation * _Nullable operation, NSError * _Nullable error, NSURL * _Nullable fileURL, NSURLResponse * _Nullable response) {
    
    if (error) {
    //Handler error
    }else{
    
    //Operation finished successfully
    }
    
    }];
    
    [operation setDownloadProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    
       NSInteger percentage = (double)totalBytesWritten * 100 / (double)totalBytesExpectedToWrite;
    
    }];

    Example Usage Case

Limit the number of concurrent or simultaneous operations


NSURL *url = [NSURL URLWithString:@"https://www.example.com/bigfile1.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

SJURLSessionOperation *operation = [[SJURLSessionOperation alloc]initWithRequest:request targetLocation:[NSURL fileURLWithPath:@"~/Desktop/bigfile1.zip"]];

NSURL *url2 = [NSURL URLWithString:@"https://www.example.com/bigfile2.zip"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];

SJURLSessionOperation *operation2 = [[SJURLSessionOperation alloc]initWithRequest:request2 targetLocation:[NSURL fileURLWithPath:@"~/Desktop/bigfile2.zip"]];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1; //limit it to one operation at a time

//Add opertations
[queue addOperation:operation];
[queue addOperation:operation2];

Acknowledgments

A lot of the source code in SJURLSessionOperation is inspired by the AFNetworking

Contact

Soneé John

SJURLSessionOperation is available under the MIT license. See the LICENSE file for more information.


Published in the hope that it will be useful
Please feel free to open a pull request

Latest podspec

{
    "name": "SJURLSessionOperation",
    "version": "1.2.0",
    "summary": "NSOperation solution for NSURLSession.",
    "description": "SJURLSessionOperation creates and manages an NSURLSessionDownloadTask object based on a specified request and download location. SJURLSessionOperation is a subclass of NSOperation which then can be used with a NSOperationQueue. In addition, it uses AFURLSessionManager so, it requires AFNetworking.",
    "homepage": "https://github.com/SoneeJohn/SJURLSessionOperation",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Soneu00e9 John": "[email protected]"
    },
    "social_media_url": "https://twitter.com/Sonee_John",
    "source": {
        "git": "https://github.com/SoneeJohn/SJURLSessionOperation.git",
        "tag": "1.2.0"
    },
    "platforms": {
        "ios": "7.0",
        "osx": "10.9"
    },
    "dependencies": {
        "AFNetworking": [
            "~> 3.0"
        ]
    },
    "source_files": "Source Files/*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This