From b3522a76e1fdd75db25090c3e98efe2c249c7ab1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 15 Jun 2022 18:33:40 -0400 Subject: [PATCH] Move CoreData store to app group --- Reader/CoreData/PersistentContainer.swift | 27 +++++++++++++++++++++++ Reader/Reader.entitlements | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/Reader/CoreData/PersistentContainer.swift b/Reader/CoreData/PersistentContainer.swift index f3cc83f..8505bad 100644 --- a/Reader/CoreData/PersistentContainer.swift +++ b/Reader/CoreData/PersistentContainer.swift @@ -33,6 +33,33 @@ class PersistentContainer: NSPersistentContainer, @unchecked Sendable { let name = account.id.base64EncodedString().replacingOccurrences(of: "/", with: "_") 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 if let error = error { fatalError("Unable to load persistent store: \(error)") diff --git a/Reader/Reader.entitlements b/Reader/Reader.entitlements index ee95ab7..eb6d285 100644 --- a/Reader/Reader.entitlements +++ b/Reader/Reader.entitlements @@ -4,6 +4,10 @@ com.apple.security.app-sandbox + com.apple.security.application-groups + + group.net.shadowfacts.Reader + com.apple.security.network.client