mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 23:03:27 +01:00
[AltStoreCore] Updates DatabaseManager to support #Preview macro
Synchronously loads database via startForPreview(), and also erases database for DEBUG builds.
This commit is contained in:
16
AltStoreCore/Extensions/ProcessInfo+Previews.swift
Normal file
16
AltStoreCore/Extensions/ProcessInfo+Previews.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// ProcessInfo+Previews.swift
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 10/11/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension ProcessInfo
|
||||
{
|
||||
var isPreview: Bool {
|
||||
ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,22 @@ public extension DatabaseManager
|
||||
|
||||
guard !self.isStarted else { return finish(nil) }
|
||||
|
||||
#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.
|
||||
@@ -166,6 +182,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 {
|
||||
|
||||
Reference in New Issue
Block a user