Skip to content

Commit

Permalink
Move pdf preloading to background thread to make the transition a bit…
Browse files Browse the repository at this point in the history
… smoother
  • Loading branch information
skywalkerdude committed Jul 10, 2020
1 parent ea069a1 commit 598dc42
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Hymns/Infrastructure/Data/PDFPreloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ protocol PDFLoader {

class PDFLoaderImpl: PDFLoader {

private let backgroundQueue: DispatchQueue
private let session: URLSession
private var cache = [URL: PDFDocument]()

init(session: URLSession = Resolver.resolve()) {
init(backgroundQueue: DispatchQueue = Resolver.resolve(name: "background"),
session: URLSession = Resolver.resolve()) {
self.backgroundQueue = backgroundQueue
self.session = session
}

/**
* Saves the `HTML` or `URL` for the current session.
*/
func load(url: URL) {
if let document = PDFDocument(url: url) {
self.cache[url] = document
backgroundQueue.async {
if let document = PDFDocument(url: url) {
self.cache[url] = document
}
}
}

Expand Down

0 comments on commit 598dc42

Please sign in to comment.