Skip to content

Commit

Permalink
Removed unneccesary failed trace from Image Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenrikur committed Aug 7, 2017
1 parent 6f8de39 commit eb0759d
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions Eurofurence/Services/ImageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ImageService: ImageServiceProtocol {
private let baseDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
private var cacheDirectory: URL
private let apiConnection: ApiConnectionProtocol
private var refreshTrace: Trace?

required init(dataContext: DataContextProtocol, apiConnection: ApiConnectionProtocol) throws {
cacheDirectory = baseDirectory.appendingPathComponent(ImageService.cacheDirectoryName, isDirectory: true)
Expand All @@ -44,16 +43,16 @@ class ImageService: ImageServiceProtocol {

func refreshCache(for images: [Image]) -> SignalProducer<Progress, ImageServiceError> {
return SignalProducer { [unowned self] observer, disposable in
self.refreshTrace = Performance.startTrace(name: "ImageService.refreshCache")
self.refreshTrace?.incrementCounter(named: "total", by: images.count)
let trace = Performance.startTrace(name: "ImageService.refreshCache")
trace?.incrementCounter(named: "total", by: images.count)
do {
try self.checkCacheDirectory()
var currentCache: [String] = try FileManager.default.contentsOfDirectory(atPath: self.cacheDirectory.path)

var producers: [SignalProducer<UIImage, ImageServiceError>] = []
for image in images {
if !self.validateCache(for: image) {
self.refreshTrace?.incrementCounter(named: "scheduled")
trace?.incrementCounter(named: "scheduled")
producers.append(self.download(image: image))
}
if let index = currentCache.index(of: image.Id) {
Expand All @@ -67,7 +66,7 @@ class ImageService: ImageServiceProtocol {

for imageId in currentCache {
self.clearCache(for: imageId)
self.refreshTrace?.incrementCounter(named: "pruned")
trace?.incrementCounter(named: "pruned")
print("Pruned image \(imageId) from cache.")
}
progress.completedUnitCount += 1
Expand All @@ -77,34 +76,29 @@ class ImageService: ImageServiceProtocol {
disposable += resultsProducer.flatten(.merge).start({ event in
switch event {
case .value:
self.refreshTrace?.incrementCounter(named: "downloaded")
trace?.incrementCounter(named: "downloaded")
progress.completedUnitCount += 1
observer.send(value: progress)
print("Image caching completed by \(progress.fractionCompleted)")
case let .failed(error):
print("Error while caching images from sync: \(error)")
self.refreshTrace?.stop()
self.refreshTrace = nil
trace?.stop()
observer.send(error: error)
case .completed:
print("Finished caching images from sync")
self.refreshTrace?.stop()
self.refreshTrace = nil
trace?.stop()
observer.sendCompleted()
case .interrupted:
print("Caching images from sync interrupted")
self.refreshTrace?.stop()
self.refreshTrace = nil
trace?.stop()
observer.sendInterrupted()
}
})
} catch let error as ImageServiceError {
self.refreshTrace?.stop()
self.refreshTrace = nil
trace?.stop()
observer.send(error: error)
} catch {
self.refreshTrace?.stop()
self.refreshTrace = nil
trace?.stop()
observer.send(error: ImageServiceError.FailedCacheDirectory(url: self.cacheDirectory, description: nil))
}
}
Expand All @@ -131,9 +125,6 @@ class ImageService: ImageServiceProtocol {
let imageServiceError = ImageServiceError.FailedDownload(image: image,
description: error.localizedDescription)
print(imageServiceError)
if let refreshTrace = self.refreshTrace {
refreshTrace.incrementCounter(named: "failed")
}
observer.sendCompleted()
}
})
Expand Down

0 comments on commit eb0759d

Please sign in to comment.