Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve read and write #10

Merged
merged 9 commits into from
Nov 12, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use write queue in disk cache add func
  • Loading branch information
vadymmarkov committed Nov 11, 2015
commit b82c6b845da49ee8ebf7f546ab677476d823bc56
24 changes: 14 additions & 10 deletions Source/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ public class DiskCache: CacheAware {
// MARK: - CacheAware

public func add<T: Cachable>(key: String, object: T, completion: (() -> Void)? = nil) {
if !fileManager.fileExistsAtPath(path) {
do {
try fileManager.createDirectoryAtPath(path,
withIntermediateDirectories: true, attributes: nil)
} catch _ {}
}
dispatch_async(writeQueue) { [weak self] in
guard let weakSelf = self else { return }

fileManager.createFileAtPath(filePath(key),
contents: object.encode(), attributes: nil)
if !weakSelf.fileManager.fileExistsAtPath(weakSelf.path) {
do {
try weakSelf.fileManager.createDirectoryAtPath(weakSelf.path,
withIntermediateDirectories: true, attributes: nil)
} catch _ {}
}

weakSelf.fileManager.createFileAtPath(weakSelf.filePath(key),
contents: object.encode(), attributes: nil)

dispatch_async(dispatch_get_main_queue()) {
completion?()
dispatch_async(dispatch_get_main_queue()) {
completion?()
}
}
}

Expand Down