From 9f03e06d430e7e4876933c919c22a07e524ab7ce Mon Sep 17 00:00:00 2001 From: kobayashi_shoto Date: Tue, 27 Aug 2024 16:56:55 +0900 Subject: [PATCH] Run runActivity on main thread (#895) Run runActivity on main thread (#895) --- Sources/SnapshotTesting/AssertSnapshot.swift | 24 +++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index 24868e082..e0c7d19cf 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -358,9 +358,11 @@ public func verifySnapshot( if !isSwiftTesting, ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") { - XCTContext.runActivity(named: "Attached Recorded Snapshot") { activity in - let attachment = XCTAttachment(contentsOfFile: snapshotFileUrl) - activity.add(attachment) + runOnMainThread { + XCTContext.runActivity(named: "Attached Recorded Snapshot") { activity in + let attachment = XCTAttachment(contentsOfFile: snapshotFileUrl) + activity.add(attachment) + } } } #endif @@ -420,9 +422,11 @@ public func verifySnapshot( if !attachments.isEmpty { #if !os(Linux) && !os(Windows) if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") { - XCTContext.runActivity(named: "Attached Failure Diff") { activity in - attachments.forEach { - activity.add($0) + runOnMainThread { + XCTContext.runActivity(named: "Attached Failure Diff") { activity in + attachments.forEach { + activity.add($0) + } } } } @@ -498,3 +502,11 @@ private class CleanCounterBetweenTestCases: NSObject, XCTestObservation { } } } + +private func runOnMainThread(action: () -> Void) { + if Thread.isMainThread { + action() + } else { + DispatchQueue.main.sync(execute: action) + } +}