Skip to content

Commit

Permalink
[Cache] removeAll: remove all files in cache path, no matter if the f…
Browse files Browse the repository at this point in the history
…ormat was registered or not
  • Loading branch information
hpique committed Jan 3, 2016
1 parent 57093de commit 4a36316
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
25 changes: 22 additions & 3 deletions Haneke/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,32 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable
}
}

public func removeAll() {
public func removeAll(completion: (() -> ())? = nil) {
let group = dispatch_group_create();
for (_, (_, memoryCache, diskCache)) in self.formats {
memoryCache.removeAllObjects()
diskCache.removeAllData()
dispatch_group_enter(group)
diskCache.removeAllData {
dispatch_group_leave(group)
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(60 * NSEC_PER_SEC))
dispatch_group_wait(group, timeout);
let path = self.cachePath
do {
try NSFileManager.defaultManager().removeItemAtPath(path)
} catch {
Log.error("Failed to remove path \(path)", error as NSError)
}
if let completion = completion {
dispatch_async(dispatch_get_main_queue()) {
completion()
}
}
}
}

// MARK: Notifications

func onMemoryWarning() {
Expand Down
7 changes: 6 additions & 1 deletion Haneke/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class DiskCache {
})
}

public func removeAllData() {
public func removeAllData(completion: (() -> ())? = nil) {
let fileManager = NSFileManager.defaultManager()
let cachePath = self.path
dispatch_async(cacheQueue, {
Expand All @@ -99,6 +99,11 @@ public class DiskCache {
} catch {
Log.error("Failed to list directory", error as NSError)
}
if let completion = completion {
dispatch_async(dispatch_get_main_queue()) {
completion()
}
}
})
}

Expand Down
29 changes: 29 additions & 0 deletions HanekeTests/CacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,35 @@ class CacheTests: XCTestCase {
}
self.waitForExpectationsWithTimeout(1, handler: nil)
}

func testRemoveAll_Completion() {
let key = self.name
sut.set(value: NSData.dataWithLength(18), key: key)
let expectation = self.expectationWithDescription("removeAll")
var completed = false
sut.removeAll {
completed = true
expectation.fulfill()
}

XCTAssertFalse(completed)
self.waitForExpectationsWithTimeout(1, handler: nil)
}

func testRemoveAll_WhenDataAlreadyPresentInCachePath() {
let path = (sut.cachePath as NSString).stringByAppendingPathComponent("test")
let data = NSData.dataWithLength(1)
data.writeToFile(path, atomically: true)
XCTAssertTrue(NSFileManager.defaultManager().fileExistsAtPath(path))

let expectation = self.expectationWithDescription("removeAll")
sut.removeAll {
expectation.fulfill()
}

self.waitForExpectationsWithTimeout(1, handler: nil)
XCTAssertFalse(NSFileManager.defaultManager().fileExistsAtPath(path))
}

func testRemoveAll_AfterNone() {
sut.removeAll()
Expand Down
18 changes: 17 additions & 1 deletion HanekeTests/DiskCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,23 @@ class DiskCacheTests: XCTestCase {
XCTAssertEqual(Int(self.sut.size), 0)
}
}


func testRemoveAllData_Completion_Filled() {
let key = self.name
let data = NSData.dataWithLength(12)
sut.setData(data, key: key)
let expectation = self.expectationWithDescription(self.name)

var completed = false
sut.removeAllData {
completed = true
expectation.fulfill()
}

XCTAssertFalse(completed)
self.waitForExpectationsWithTimeout(1, handler: nil)
}

func testRemoveAllData_Empty() {
let key = self.name
let path = sut.pathForKey(key)
Expand Down

0 comments on commit 4a36316

Please sign in to comment.