Skip to content

Commit

Permalink
Isolation cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed Sep 25, 2024
1 parent f5fff90 commit fd7146c
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 87 deletions.
3 changes: 3 additions & 0 deletions Gladys.xcodeproj/xcshareddata/xcschemes/MacGladys.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
disableMainThreadChecker = "YES"
disablePerformanceAntipatternChecker = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
migratedStopOnEveryIssue = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<key>GladysAction.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
<integer>2</integer>
</dict>
<key>GladysFiles.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -22,22 +22,22 @@
<key>GladysIndex.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>1</integer>
</dict>
<key>GladysIntents.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>6</integer>
<integer>4</integer>
</dict>
<key>GladysKeys.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>11</integer>
<integer>9</integer>
</dict>
<key>GladysMessage.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>8</integer>
<integer>6</integer>
</dict>
<key>GladysMirror.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -47,12 +47,12 @@
<key>GladysToday.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>5</integer>
<integer>3</integer>
</dict>
<key>GladysWatch.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>7</integer>
<integer>5</integer>
</dict>
<key>GladysWidgetsExtension.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -62,12 +62,12 @@
<key>MacGladys.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>12</integer>
<integer>10</integer>
</dict>
<key>MacGladysHelper.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>10</integer>
<integer>8</integer>
</dict>
<key>MacGladysIndex.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -77,12 +77,12 @@
<key>MacGladysShare.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>9</integer>
<integer>7</integer>
</dict>
<key>MacGladysWidgetsExtension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>13</integer>
<integer>12</integer>
</dict>
<key>Playground-iOS (Playground) 1.xcscheme</key>
<dict>
Expand Down
2 changes: 2 additions & 0 deletions Gladys/Logic/WatchDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ final class WatchDelegate: NSObject, WCSessionDelegate {
let session = WCSession.default
guard session.isReachable, session.activationState == .activated, session.isPaired, session.isWatchAppInstalled else { return }
let context = Self.buildContext()
Maintini.startMaintaining()
Task.detached {
_ = try? await session.sendWatchMessage(context)
await Maintini.endMaintaining()
}
log("Updated watch context")
}
Expand Down
4 changes: 2 additions & 2 deletions Gladys/UI/Detail/HeaderCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class HeaderCell: UITableViewCell, UITextViewDelegate {
label.textContainerInset = .zero
observer = label.observe(\.selectedTextRange, options: .new) { [weak self] _, _ in
guard let self else { return }
Task { @MainActor in
self.caretMoved()
Task {
await self.caretMoved()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Gladys/UI/Detail/NoteCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final class NoteCell: UITableViewCell, UITextViewDelegate {
placeholder.textColor = c
observer = textView.observe(\.selectedTextRange, options: .new) { [weak self] _, _ in
guard let self else { return }
Task { @MainActor in
self.caretMoved()
Task {
await self.caretMoved()
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions Gladys/UI/Main/Reachability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ final actor Reachability {
}
lastStatus = newName
log("Reachability update: \(newName)")
Task { @MainActor in
sendNotification(name: .ReachabilityChanged, object: nil)
}
sendNotification(name: .ReachabilityChanged, object: nil)
}

var isReachableViaLowCost: Bool {
Expand Down
4 changes: 2 additions & 2 deletions Gladys/UI/Prefs/PreferencesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ final class PreferencesController: GladysViewController, UIDragInteractionDelega
var cancelled = false
let progress = p.loadFileRepresentation(forTypeIdentifier: GladysFileUTI) { url, error in
if onlyOnMainThread({ cancelled }) {
Task { @MainActor in
self.updateUI()
Task {
await self.updateUI()
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions GladysCommon/Sources/GladysCommon/CollectionHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public extension Collection where Element: Hashable {
return filter { set.insert($0).inserted }
}

func asyncMap<T>(block: (Element) async -> T) async -> [T] {
func asyncMap<T>(block: @Sendable (Element) async -> T) async -> [T] {
var result = [T]()
result.reserveCapacity(count)
for element in self {
Expand All @@ -17,7 +17,7 @@ public extension Collection where Element: Hashable {
return result
}

func asyncCompactMap<T>(block: (Element) async -> T?) async -> [T] {
func asyncCompactMap<T>(block: @Sendable (Element) async -> T?) async -> [T] {
var result = [T]()
result.reserveCapacity(count)
for element in self {
Expand All @@ -28,7 +28,7 @@ public extension Collection where Element: Hashable {
return result
}

func asyncFilter(block: (Element) async -> Bool) async -> [Element] {
func asyncFilter(block: @Sendable (Element) async -> Bool) async -> [Element] {
var result = [Element]()
result.reserveCapacity(count)
for element in self where await block(element) {
Expand Down
2 changes: 1 addition & 1 deletion GladysCommon/Sources/GladysCommon/GladysCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public let isRunningInTestFlightEnvironment: Bool = {
#endif
}()

public func onlyOnMainThread<T: Sendable>(_ block: @MainActor () throws -> T) rethrows -> T {
public func onlyOnMainThread<T: Sendable>(_ block: @Sendable @MainActor () throws -> T) rethrows -> T {
if Thread.isMainThread {
try MainActor.assumeIsolated {
try block()
Expand Down
5 changes: 3 additions & 2 deletions GladysCommon/Sources/GladysCommon/HighlightRequest.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Foundation

@MainActor
public protocol HighlightListener: AnyObject, Sendable {
func highlightItem(request: HighlightRequest) async
}

@MainActor
public struct HighlightRequest: Sendable {
public enum Action: Sendable {
case none, detail, open, preview(String?), userDefault
Expand Down Expand Up @@ -33,14 +31,17 @@ public struct HighlightRequest: Sendable {
}
}

@MainActor
private static var registrations = Set<Registration>()

@MainActor
public static func registerListener(listener: some HighlightListener) -> Registration {
let registration = Registration(listener: listener)
registrations.insert(registration)
return registration
}

@MainActor
public static func send(uuid: String, extraAction: Action) {
let request = HighlightRequest(uuid: uuid, extraAction: extraAction)
Task {
Expand Down
2 changes: 1 addition & 1 deletion GladysCommon/Sources/GladysCommon/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public func sendNotification(name: Notification.Name, object: Sendable? = nil) {
}

public func notifications(for name: Notification.Name, block: @MainActor @escaping (Any?) async -> Void) {
Task { @MainActor in
Task {
for await notification in NotificationCenter.default.notifications(named: name) {
let obj = notification.object
await block(obj)
Expand Down
4 changes: 3 additions & 1 deletion GladysCommon/Sources/GladysCommon/PersistedOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ public enum DefaultTapAction: Int, Sendable {
case infoPanel = 0, preview, open, copy, none
}

extension UserDefaults: @retroactive @unchecked Sendable {}

@MainActor
public enum PersistedOptions {
public nonisolated(unsafe) static let defaults = UserDefaults(suiteName: groupName)!
public nonisolated static let defaults = UserDefaults(suiteName: groupName)!

private static var wideModeCache: Bool?
public static var wideMode: Bool {
Expand Down
4 changes: 2 additions & 2 deletions GladysKeys/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ final class KeyboardViewController: UIInputViewController, UICollectionViewDeleg
textDocumentProxy.deleteBackward()
backspaceTimer = Timer.scheduledTimer(withTimeInterval: 0.7, repeats: false) { [weak self] _ in
guard let self else { return }
Task { @MainActor in
self.startRapidBackspace()
Task {
await startRapidBackspace()
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions GladysUI/Sources/GladysUI/CloudManager+Mainapps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public extension CloudManager {

static var showNetwork = false {
didSet {
Task { @MainActor in
Model.updateBadge()
Task {
await Model.updateBadge()
}
}
}
Expand Down Expand Up @@ -363,8 +363,8 @@ public extension CloudManager {
lastSyncCompletion = .distantPast
uuidSequence = []
uuidSequenceRecord = nil
PullState.wipeDatabaseTokens()
PullState.wipeZoneTokens()
await PullState.wipeDatabaseTokens()
await PullState.wipeZoneTokens()
await Model.removeImportedShares()
syncSwitchedOn = false
lastiCloudAccount = nil
Expand Down
10 changes: 3 additions & 7 deletions GladysUI/Sources/GladysUI/Coordination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import Foundation
let presentedItemOperationQueue = OperationQueue()

func presentedItemDidChange() {
Task { @MainActor in
if !DropStore.ingestingItems {
try! await Model.reloadDataIfNeeded()
}
Task {
try! await Model.reloadIfPossible()
}
}
}
Expand All @@ -36,9 +34,7 @@ import Foundation
public static func beginMonitoringChanges() {
notifications(for: UIApplication.willEnterForegroundNotification) { _ in
NSFileCoordinator.addFilePresenter(filePresenter)
if !DropStore.ingestingItems {
try! await Model.reloadDataIfNeeded()
}
try! await Model.reloadIfPossible()
}

notifications(for: UIApplication.didEnterBackgroundNotification) { _ in
Expand Down
4 changes: 2 additions & 2 deletions GladysUI/Sources/GladysUI/ImportExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum ImportExport {
}.value
}

private nonisolated static func createArchiveThread(progress p: Progress, eligibleItems: [ArchivedItem]) throws -> URL {
private static func createArchiveThread(progress p: Progress, eligibleItems: [ArchivedItem]) throws -> URL {
let fm = FileManager()
let tempPath = temporaryDirectoryUrl.appendingPathComponent("Gladys Archive.gladysArchive")
let path = tempPath.path
Expand Down Expand Up @@ -70,7 +70,7 @@ public enum ImportExport {
}.value
}

private nonisolated static func createZipThread(dropsCopy: ContiguousArray<ArchivedItem>, progress p: Progress) async throws -> URL {
private static func createZipThread(dropsCopy: ContiguousArray<ArchivedItem>, progress p: Progress) async throws -> URL {
let tempPath = temporaryDirectoryUrl.appendingPathComponent("Gladys.zip")

let fm = FileManager.default
Expand Down
7 changes: 7 additions & 0 deletions GladysUI/Sources/GladysUI/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public enum Model {
}.value
}

public static func reloadIfPossible() async throws {
if DropStore.ingestingItems {
return
}
try await reloadDataIfNeeded()
}

private nonisolated static func shouldLoad(from url: URL) -> Bool {
onlyOnMainThread {
guard let dataModified = modificationDate(for: url), dataModified != dataFileLastModified else {
Expand Down
9 changes: 8 additions & 1 deletion GladysUI/Sources/GladysUI/PullState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import GladysCommon
import Lista
import PopTimer

final actor PullState {
@globalActor
public enum SyncActor {
public final actor ActorType {}
public static let shared = ActorType()
}

@SyncActor
final class PullState {
private enum ZoneModification {
case itemModified(modification: CKDatabase.RecordZoneChange.Modification)
case itemDeleted(deletion: CKDatabase.RecordZoneChange.Deletion)
Expand Down
Loading

0 comments on commit fd7146c

Please sign in to comment.