[AltStoreCore] Updates DatabaseManager to support #Preview macro

Synchronously loads database via startForPreview(), and also erases database for DEBUG builds.
This commit is contained in:
Riley Testut
2023-10-11 17:59:01 -05:00
committed by Magesh K
parent 1ece687e37
commit 9ddc27f6ca
3 changed files with 54 additions and 2 deletions

View File

@@ -86,8 +86,24 @@ public extension DatabaseManager
guard !self.isStarted else { return finish(nil) }
if self.persistentContainer.isMigrationRequired {
#if DEBUG
// Wrap in #if DEBUG to *ensure* we never accidentally delete production databases.
if ProcessInfo.processInfo.isPreview
{
do
{
print("!!! Purging database for preview...")
try FileManager.default.removeItem(at: PersistentContainer.defaultDirectoryURL())
}
catch
{
print("Failed to remove database directory for preview.", error)
}
}
#endif
if self.persistentContainer.isMigrationRequired
{
// Quit any other running AltStore processes to prevent concurrent database access during and after migration.
self.ignoreWillMigrateDatabaseNotification = true
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), .willMigrateDatabase, nil, nil, true)
@@ -164,6 +180,22 @@ public extension DatabaseManager
}
}
public extension DatabaseManager
{
func startForPreview()
{
let semaphore = DispatchSemaphore(value: 0)
self.dispatchQueue.async {
self.startCompletionHandlers.append { error in
semaphore.signal()
}
}
semaphore.wait()
}
}
public extension DatabaseManager
{
var viewContext: NSManagedObjectContext {