Skip to content

fix: remove caching of files #414

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

Merged
merged 3 commits into from
Sep 14, 2022
Merged

fix: remove caching of files #414

merged 3 commits into from
Sep 14, 2022

Conversation

cbaker6
Copy link
Contributor

@cbaker6 cbaker6 commented Sep 14, 2022

New Pull Request Checklist

Issue Description

Accessing a cached file does not result in the actual file. This is due to the OS caching policy not directly having a way to cache URLSessionDownloadTask. In addition, caching files is most likely filling up the cache too quickly, removing queries and other data from cache earlier than expected.

Related issue: #413, #226

Approach

Remove file caching from the SDK. If developers want to check if a file has already been saved they can check the download directory for the file name. Below is sample code for reference:

@MainActor
    static func fetchImage(_ file: ParseFile?) async -> UIImage? {
        let defaultImage = UIImage(systemName: "camera")
        guard let file = file,
              let fileManager = ParseFileManager(),
              let defaultDirectoryPath = fileManager.defaultDataDirectoryPath else {
            return defaultImage
        }
        let downloadDirectoryPath = defaultDirectoryPath
            .appendingPathComponent(Constants.fileDownloadsDirectory, isDirectory: true)
        let fileLocation = downloadDirectoryPath.appendingPathComponent(file.name).relativePath
        if FileManager.default.fileExists(atPath: fileLocation) {
            guard let image = UIImage(contentsOfFile: fileLocation) else {
                do {
                    let image = try await file.fetch()
                    if let path = image.localURL?.relativePath,
                       let image = UIImage(contentsOfFile: path) {
                        return image
                    } else {
                        return defaultImage
                    }
                } catch {
                    Logger.home.error("Error fetching picture: \(error.localizedDescription)")
                    return defaultImage
                }
            }
            return image
        } else {
            guard let path = file.localURL?.relativePath,
                  let image = UIImage(contentsOfFile: path) else {
                      do {
                          let image = try await file.fetch()
                          if let path = image.localURL?.relativePath,
                                let image = UIImage(contentsOfFile: path) {
                              return image
                          } else {
                              return defaultImage
                          }
                    } catch {
                        Logger.home.error("Error fetching picture: \(error.localizedDescription)")
                        return defaultImage
                    }
            }
            return image
        }
    }

TODOs before merging

  • Add tests
  • Add entry to changelog

@parse-github-assistant
Copy link

parse-github-assistant bot commented Sep 14, 2022

Thanks for opening this pull request!

  • 🎉 We are excited about your hands-on contribution!

@cbaker6 cbaker6 linked an issue Sep 14, 2022 that may be closed by this pull request
4 tasks
@codecov
Copy link

codecov bot commented Sep 14, 2022

Codecov Report

Base: 90.01% // Head: 90.06% // Increases project coverage by +0.04% 🎉

Coverage data is based on head (1511564) compared to base (baf70f6).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #414      +/-   ##
==========================================
+ Coverage   90.01%   90.06%   +0.04%     
==========================================
  Files         159      159              
  Lines       15323    15308      -15     
==========================================
- Hits        13793    13787       -6     
+ Misses       1530     1521       -9     
Impacted Files Coverage Δ
Sources/ParseSwift/Extensions/URLSession.swift 83.39% <ø> (+1.25%) ⬆️
Sources/ParseSwift/Objects/ParseUser.swift 87.26% <0.00%> (+0.17%) ⬆️
Sources/ParseSwift/API/API+Command.swift 89.81% <0.00%> (+0.20%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@cbaker6 cbaker6 merged commit e54a22c into parse-community:main Sep 14, 2022
@cbaker6 cbaker6 deleted the caching branch September 14, 2022 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Second time fetched file is broken
1 participant