Move CoreData store to app group

This commit is contained in:
Shadowfacts 2022-06-15 18:33:40 -04:00
parent 197edc5e1a
commit b3522a76e1
2 changed files with 31 additions and 0 deletions

View File

@ -33,6 +33,33 @@ class PersistentContainer: NSPersistentContainer, @unchecked Sendable {
let name = account.id.base64EncodedString().replacingOccurrences(of: "/", with: "_") let name = account.id.base64EncodedString().replacingOccurrences(of: "/", with: "_")
super.init(name: name, managedObjectModel: PersistentContainer.managedObjectModel) super.init(name: name, managedObjectModel: PersistentContainer.managedObjectModel)
let groupContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.net.shadowfacts.Reader")!
let containerAppSupportURL = groupContainerURL
.appendingPathComponent("Library", isDirectory: true)
.appendingPathComponent("Application Support", isDirectory: true)
try! FileManager.default.createDirectory(at: containerAppSupportURL, withIntermediateDirectories: true)
let containerStoreURL = containerAppSupportURL
.appendingPathComponent(name)
.appendingPathExtension("sqlite")
if let existingAppSupport = try? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
let existingStore = existingAppSupport
.appendingPathComponent(name)
.appendingPathExtension("sqlite")
.relativePath
.removingPercentEncoding!
if FileManager.default.fileExists(atPath: existingStore) {
for ext in ["", "-shm", "-wal"] {
try! FileManager.default.moveItem(atPath: existingStore + ext, toPath: containerStoreURL.relativePath + ext)
}
}
}
let desc = NSPersistentStoreDescription(url: containerStoreURL)
desc.type = NSSQLiteStoreType
persistentStoreDescriptions = [desc]
loadPersistentStores { description, error in loadPersistentStores { description, error in
if let error = error { if let error = error {
fatalError("Unable to load persistent store: \(error)") fatalError("Unable to load persistent store: \(error)")

View File

@ -4,6 +4,10 @@
<dict> <dict>
<key>com.apple.security.app-sandbox</key> <key>com.apple.security.app-sandbox</key>
<true/> <true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.net.shadowfacts.Reader</string>
</array>
<key>com.apple.security.network.client</key> <key>com.apple.security.network.client</key>
<true/> <true/>
</dict> </dict>