[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
parent 26a05d323f
commit 03fad2f202
3 changed files with 52 additions and 0 deletions

View File

@@ -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 {