9

I know that SDWebImage loads the image in a background thread so you're not blocking the UI/main thread when this downloading is going on. Furthermore, it will also disk-cache all the images you've downloaded and will NEVER re-download an image from the same URL.

So I wonder if there is something similar or the same for videos?

Something to note: I add Videos as Sublayer.

let videoURL = URL(string: postArray[indexPath.item].media[0].videoURLString!)//need to do error handlin here
print(videoURL as Any, "<-- video url in dispkay")

let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x: -8, y: 0, width: 138, height: 217)//cell.frame


cell.imageOrVideoView.layer.addSublayer(playerLayer)
//Other code and play()

This was recommended in the past but it seems like it does something different or at the very leased has too much extra functionality I dont need.

Update:

What I am testing:

  DispatchQueue.global(qos: .default).async(execute: {

            var downloadedData: Data? = nil
            if let url = URL(string: videoURL) {
                do {
                    downloadedData = try Data(contentsOf: url)
                } catch {
                    print(error, "downloaded Data failed")
                }
            }

            if downloadedData != nil {

                // STORE IN FILESYSTEM
                var cachesDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
                var file = URL(fileURLWithPath: cachesDirectory).appendingPathComponent(videoURL).absoluteString
                do {
                    try downloadedData?.write(to: URL(string: file)!)
                } catch {
                    print(error, "error dowloading data and writing it")
                }

                // STORE IN MEMORY
                if let downloadedData = downloadedData {
                    memoryCache?.setObject(downloadedData as AnyObject, forKey: videoURL as AnyObject)
                }
            }

            // NOW YOU CAN CREATE AN AVASSET OR UIIMAGE FROM THE FILE OR DATA
        })

I do not understand however if I should do something right after the last line or if I should do it after the }) or if I need to add a Update UI there.

  • 1
    Please make your question stand on its own. What is `imageUrlString`? – rmaddy Apr 23 '19 at 20:44
  • 4
    What algorithm? Your question doesn't explain what your code is supposed to do. Show a clear example of the input and the desired output and explain the logic. – rmaddy Apr 23 '19 at 23:19

1 Answers1

9

So I was able to solve the problem with the following:

Swift 4:

import Foundation

public enum Result<T> {
    case success(T)
    case failure(NSError)
}

class CacheManager {

    static let shared = CacheManager()
    private let fileManager = FileManager.default
    private lazy var mainDirectoryUrl: URL = {

    let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
        return documentsUrl
    }()

    func getFileWith(stringUrl: String, completionHandler: @escaping (Result<URL>) -> Void ) {

        let file = directoryFor(stringUrl: stringUrl)

        //return file path if already exists in cache directory
        guard !fileManager.fileExists(atPath: file.path)  else {
            completionHandler(Result.success(file))
            return
        }

        DispatchQueue.global().async {

            if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
                videoData.write(to: file, atomically: true)
                DispatchQueue.main.async {
                    completionHandler(Result.success(file))
                }
            } else {
                DispatchQueue.main.async {
                    let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])

                    completionHandler(Result.failure(error))
                }
            }
        }
    }

    private func directoryFor(stringUrl: String) -> URL {

        let fileURL = URL(string: stringUrl)!.lastPathComponent
        let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)
        return file
    }
}

Usage:

      CacheManager.shared.getFileWith(stringUrl: videoURL) { result in

        switch result {
        case .success(let url):
        // do some magic with path to saved video

            break;
        case .failure(let error):
            // handle errror

            print(error, " failure in the Cache of video")
            break;
        }
    }
  • 1
    Actually, I think It is better to run DispatchQueue on background thread with DispatchQueue.global(qos: .background).async { } – Emre Önder Apr 30 '19 at 08:07