From 266ac1ee501dd1f53c0ef9922c327b22f81d21c0 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 7 Nov 2022 15:26:04 -0600 Subject: [PATCH] Limits quitting other AltStore processes to database migrations only Previously, AltStore would quit all other processes when first accessing the database no matter what. However, this unintentionally caused the widget extension to quit the main app after refreshing apps. Now, we only quit other AltStore processes if a database migration is required. This still prevents multiple AltStores with different database schemas from accessing database concurrently, but also allows extensions to access database without quitting main app. --- AltStoreCore/Model/DatabaseManager.swift | 25 +++++++++++++----------- Dependencies/Roxas | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/AltStoreCore/Model/DatabaseManager.swift b/AltStoreCore/Model/DatabaseManager.swift index b91a7e8e..3d443a62 100644 --- a/AltStoreCore/Model/DatabaseManager.swift +++ b/AltStoreCore/Model/DatabaseManager.swift @@ -13,11 +13,11 @@ import Roxas extension CFNotificationName { - fileprivate static let willAccessDatabase = CFNotificationName("com.rileytestut.AltStore.WillAccessDatabase" as CFString) + fileprivate static let willMigrateDatabase = CFNotificationName("com.rileytestut.AltStore.WillMigrateDatabase" as CFString) } -private let ReceivedWillAccessDatabaseNotification: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void = { (center, observer, name, object, userInfo) in - DatabaseManager.shared.receivedWillAccessDatabaseNotification() +private let ReceivedWillMigrateDatabaseNotification: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void = { (center, observer, name, object, userInfo) in + DatabaseManager.shared.receivedWillMigrateDatabaseNotification() } fileprivate class PersistentContainer: RSTPersistentContainer @@ -52,7 +52,7 @@ public class DatabaseManager private let coordinator = NSFileCoordinator() private let coordinatorQueue = OperationQueue() - private var ignoreWillAccessDatabaseNotification = false + private var ignoreWillMigrateDatabaseNotification = false private init() { @@ -60,7 +60,7 @@ public class DatabaseManager self.persistentContainer.preferredMergePolicy = MergePolicy() let observer = Unmanaged.passUnretained(self).toOpaque() - CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, ReceivedWillAccessDatabaseNotification, CFNotificationName.willAccessDatabase.rawValue, nil, .deliverImmediately) + CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, ReceivedWillMigrateDatabaseNotification, CFNotificationName.willMigrateDatabase.rawValue, nil, .deliverImmediately) } } @@ -87,9 +87,12 @@ public extension DatabaseManager guard !self.isStarted else { return finish(nil) } - // Quit any other running AltStore processes to prevent concurrent database access during and after migration. - self.ignoreWillAccessDatabaseNotification = true - CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), .willAccessDatabase, nil, nil, true) + 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) + } self.migrateDatabaseToAppGroupIfNeeded { (result) in switch result @@ -417,12 +420,12 @@ private extension DatabaseManager } } - func receivedWillAccessDatabaseNotification() + func receivedWillMigrateDatabaseNotification() { - defer { self.ignoreWillAccessDatabaseNotification = false } + defer { self.ignoreWillMigrateDatabaseNotification = false } // Ignore notifications sent by the current process. - guard !self.ignoreWillAccessDatabaseNotification else { return } + guard !self.ignoreWillMigrateDatabaseNotification else { return } exit(104) } diff --git a/Dependencies/Roxas b/Dependencies/Roxas index cafe3d45..50d73df2 160000 --- a/Dependencies/Roxas +++ b/Dependencies/Roxas @@ -1 +1 @@ -Subproject commit cafe3d45e07bbdc18e55a6aedb2f63a6dceed0e0 +Subproject commit 50d73df231d07c516207d33648b759307fcccb25