Skip to content

Instantly share code, notes, and snippets.

@longvudai
Created July 10, 2022 06:37
Show Gist options
  • Save longvudai/acb5984aefa7164aa3931bddb9877dcf to your computer and use it in GitHub Desktop.
Save longvudai/acb5984aefa7164aa3931bddb9877dcf to your computer and use it in GitHub Desktop.
import Foundation
final class PersistenceSyncManager {
private let fileManager: FileManager = .default
private let sharedContainer: URL
private let sharedTargetPath: String = "YOUR-SHARED-FOLDER"
private var contentContainerURL: URL { sharedContainer.appendingPathComponent(sharedTargetPath) }
static let shared = PersistenceSyncManager()
private convenience init() {
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "YOUR-APP-GROUP") else {
fatalError()
}
self.init(sharedContainer: containerURL)
}
private init(sharedContainer: URL) {
self.sharedContainer = sharedContainer
}
func copyContentsToSharedTarget(from url: URL) throws {
if fileManager.fileExists(atPath: contentContainerURL.path) {
try fileManager.removeItem(at: contentContainerURL)
}
try fileManager.copyItem(atPath: url.path, toPath: contentContainerURL.path)
}
func copyContentsFromSharedTarget(to url: URL) throws {
if !fileManager.fileExists(atPath: contentContainerURL.path) {
return
}
if fileManager.fileExists(atPath: url.path) {
try fileManager.removeItem(at: url)
}
try fileManager.copyItem(atPath: contentContainerURL.path, toPath: url.path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment