Skip to content

Commit

Permalink
Merge pull request #81 from 3lvis/refactor/download-data
Browse files Browse the repository at this point in the history
Refactor download data
  • Loading branch information
3lvis committed May 9, 2016
2 parents c1e3d03 + 5c97218 commit 4e04880
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
49 changes: 24 additions & 25 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,40 +202,39 @@ public class Networking {
}
}
}



/**
Downloads data from a URL, caching the result.
- Careful: Doesn't create a new task if current task with same URL is already running.
- parameter URL: the data URL.
- parameter completion: the completion handler.
- parameter path: The path used to download the resource
- parameter completion: A closure that gets called when the download request is completed, it contains a `data` object and a `NSError`.
*/
public func downloadData(URL: NSURL, completion: (data: NSData?, error: NSError?) -> ()) {
let semaphore = dispatch_semaphore_create(0)

if let _ = self.cache.objectForKey(String(format: "task: ", URL.absoluteString)) {
// NSLog("Task for this URL already processing")
} else if let data = self.cache.objectForKey(URL.absoluteString) as? NSData {
// NSLog("Cached data")
self.cache.removeObjectForKey(String(format: "task: ", URL.absoluteString))
completion(data: data, error: nil)
public func downloadData(path: String, completion: (data: NSData?, error: NSError?) -> Void) {
let url = self.urlForPath(path)
if let cachedData = self.cache.objectForKey(url.absoluteString) as? NSData {
completion(data: cachedData, error: nil)
} else {
// NSLog("Fetche data")
let task = self.session.dataTaskWithURL(URL) { (data, response, err) in
if let data = data {
self.cache.setObject(data, forKey: URL.absoluteString)
self.cache.removeObjectForKey(String(format: "task: ", URL.absoluteString))
completion(data: data, error: err)
var found = false
let semaphore = dispatch_semaphore_create(0)
self.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
for downloadTask in downloadTasks where downloadTask.originalRequest?.URL?.absoluteString == url.absoluteString {
found = true
}
}

if !found {
self.session.dataTaskWithURL(url) { data, response, error in
if let data = data {
self.cache.setObject(data, forKey: url.absoluteString)
}
completion(data: data, error: error)
if TestCheck.isTesting {
dispatch_semaphore_signal(semaphore)
}
}.resume()
if TestCheck.isTesting {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}
}
self.cache.setObject(task, forKey: String(format: "task: ", URL.absoluteString))
task.resume()
if TestCheck.isTesting {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions Tests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,13 @@ class NetworkingTests: XCTestCase {
}

func testDownloadData() {
// for now we don't need a baseURL, should probably make this optional or add new initialiser
let networking = Networking(baseURL: "")
let path = self.baseURL + "/image/png"
let URL = NSURL(string: path)!
networking.downloadData(URL) { (data, error) in
if let data = data {
XCTAssertNotNil(data)
XCTAssertTrue(data.length == 8090, "Data size mismatch")
} else {
XCTFail("Data is nil")
}
var synchronous = false
let networking = Networking(baseURL: self.baseURL)
networking.downloadData("/image/png") { data, error in
synchronous = true
XCTAssertNotNil(data)
XCTAssertTrue(data!.length == 8090, "Data size mismatch")
}
XCTAssertTrue(synchronous)
}
}

0 comments on commit 4e04880

Please sign in to comment.