Created
July 10, 2022 06:37
-
-
Save longvudai/acb5984aefa7164aa3931bddb9877dcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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