Skip to content

Can you provide an example of how to use it? I don't seem to know how to use it. #7

Open

Description

@emerana
No description provided.

Activity

  1. tomatrow commented on Jan 6, 2021

    @tomatrow

    So, here's an example:

    // main.swift
    
    let imageCache = MemoryCache<NSData>(countLimit: 50, automaticallyRemoveAllObjects: true)
    
    func imageApiCall(key: String, completion: @escaping (NSData) -> ()) {
        // some expensive code
    }
    
    func fetchImage(key: String, completion: @escaping (NSData) -> ()) {
        imageCache.get(key: key) { cachedData in
            if let data = cachedData {
                completion(data)
            } else {
                imageApiCall(key: key) { requestedData in 
                    imageCache.set(key: key, value: requestedData)
                    completion(requestedData)
                }
            }
        }
    }

    P.S Here's a class that lets you cache a wider range of objects.

    Also, the author is pretty great. Check out this iOS related podcast he did a while back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions