Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Mar 28, 2015
1 parent 0febdf2 commit c4834ce
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ViewController: UIViewController {
@IBAction func didPressedStartButton(sender: AnyObject) {
let downloadUrl1 = "https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf"
let downloadUrl2 = "https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf"
let downloadUrl3 = "https://s3.amazonaws.com/hayageek/downloads/SimpleBackgroundFetch.zip"
let downloadUrl3 = "https://developer.apple.com/library/prerelease/ios/releasenotes/DeveloperTools/RN-Xcode/Xcode_Release_Notes.pdf"
let uploadUrl = "http:https://httpbin.org/post"
let documentsPath: AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let des = NSURL(string: documentsPath.stringByAppendingString("/file.pdf"))!
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ Features
-----

- multiple files (parallel, serial)
- progress
- background uploads and downloads
- progress tracking
- retry
- timeout
- header settings
- background
- LlamaKit (https://github.com/LlamaKit/LlamaKit)


**Quick example**
Expand All @@ -42,22 +41,25 @@ let task = UploadTask(url: "http:https://server.com", file: fileUrl)
}


Transporter.add(task1 --> task2 --> task3)
Transporter.add(task1 <--> task2 <--> task3) // concurrent tasks
.progress { bytes, total in
let ratio = Double(bytes) / Double(total)
println("serial tasks: \(ratio)")
let per = Double(bytes) / Double(total)
println("serial tasks: \(per)")
}
.completed {
println("task1, task2, task3: completed")
}
.add(task4 <--> task5)
.add(task4 --> task5 --> task6) // serial tasks
.progress { bytes, total in
println("concurrent tasks")
}
.resume()

```

Usage
-----

``` swift
// downloading task

Expand Down Expand Up @@ -88,8 +90,8 @@ let task = UploadTask(url: "http:https://server.com", data: uploadData)

task.headers = ["key": "value"]
task.params = ["key": "value"]
task.suspend
task.cancel
task.pause()
task.cancel()
task.retry

// background handling
Expand Down
9 changes: 8 additions & 1 deletion Transporter/TPTaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,16 @@ public class TPTaskGroup : TPTask {
}
}


extension TPTaskGroup : NSURLSessionDelegate {
// All tasks enqueued have been delivered
public func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
Transporter.sessionDidFinishEventsForBackgroundURLSession(session)
// check if all tasks have been completed
session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
if dataTasks.isEmpty && uploadTasks.isEmpty && downloadTasks.isEmpty {
Transporter.sessionDidFinishEventsForBackgroundURLSession(session)
}
}
}
}

Expand Down Expand Up @@ -175,6 +181,7 @@ extension TPTaskGroup : NSURLSessionTaskDelegate {
}
}


extension TPTaskGroup : NSURLSessionDownloadDelegate {
// The download task has resumed downloading
public func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
Expand Down
2 changes: 1 addition & 1 deletion Transporter/Transporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation

public struct Transporter {
public static var headers: [String: String]?
public static var timeoutIntervalForRequest = 30.0
public static var timeoutIntervalForRequest = 60.0
public static var timeoutIntervalForResource = 60.0
public static var HTTPMaximumconnectionsPerHost: Int = 5

Expand Down

0 comments on commit c4834ce

Please sign in to comment.