diff --git a/Sources/Core/Tracker/Tracker.swift b/Sources/Core/Tracker/Tracker.swift index 087d3c330..c399d5e01 100644 --- a/Sources/Core/Tracker/Tracker.swift +++ b/Sources/Core/Tracker/Tracker.swift @@ -431,13 +431,9 @@ class Tracker: NSObject { var components = URLComponents(url: url, resolvingAgainstBaseURL: false) let spQueryParam = URLQueryItem(name: "_sp", value: spParameters) - if let index = components?.queryItems?.firstIndex(where: { $0.name == "_sp" }) { - // Replace the old query item with the new one - components?.queryItems?[index] = spQueryParam - } else { - let queryItems = components?.queryItems ?? [] - components?.queryItems = queryItems + [spQueryParam] - } + // Modification requires exclusive access, we must make a copy + var queryItems = components?.queryItems + components?.queryItems = (queryItems?.filter { $0.name != "_sp" } ?? []) + [spQueryParam] return components?.url } diff --git a/Tests/TestLinkDecorator.swift b/Tests/TestLinkDecorator.swift index 3b0fb9445..9bca2a119 100644 --- a/Tests/TestLinkDecorator.swift +++ b/Tests/TestLinkDecorator.swift @@ -114,7 +114,7 @@ class TestLinkDecorator: XCTestCase { let result = tracker.decorateLink(link, extendedParameters: CrossDeviceParameterConfiguration(sessionId: false, sourceId: false))! - matches(for: "https://example.com?a=a&_sp=\(tracker.session!.userId!).\(epoch)&b=b", in: result.absoluteString) + matches(for: "https://example.com?a=a&b=b&_sp=\(tracker.session!.userId!).\(epoch)&b=b", in: result.absoluteString) } func testMissingFields() {