From ed048a09c376ab9166f4f8d84f7fcd27e6ef0fa5 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Mon, 15 Sep 2025 12:05:46 +0200 Subject: [PATCH] fix --- Sources/NextcloudKit/NextcloudKit.swift | 32 ++++++++++++------- .../Utils/SynchronizedNKSessionArray.swift | 17 ++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Sources/NextcloudKit/NextcloudKit.swift b/Sources/NextcloudKit/NextcloudKit.swift index bc260496..ae7d63c2 100644 --- a/Sources/NextcloudKit/NextcloudKit.swift +++ b/Sources/NextcloudKit/NextcloudKit.swift @@ -108,35 +108,45 @@ open class NextcloudKit { nkCommonInstance.nksessions.append(nkSession) } + /// Updates an existing `NKSession` stored in the synchronized array. + /// + /// This method looks up the session by its `account` identifier, applies any non-nil + /// parameters to mutate the session, and then replaces the stored value using + /// `SynchronizedNKSessionArray.replace(account:with:)`. + /// + /// - Parameters: + /// - account: The account identifier used to locate the session to update. + /// - urlBase: An optional new base URL for the session. + /// - user: An optional new username for the session. + /// - userId: An optional new user identifier for the session. + /// - password: An optional new password or token for the session. + /// - userAgent: An optional new User-Agent string for the session. public func updateSession(account: String, urlBase: String? = nil, user: String? = nil, userId: String? = nil, password: String? = nil, - userAgent: String? = nil, - replaceWithAccount: String? = nil) { - guard var nkSession = nkCommonInstance.nksessions.session(forAccount: account) else { + userAgent: String? = nil) { + guard var newSession = nkCommonInstance.nksessions.session(forAccount: account) else { return } if let urlBase { - nkSession.urlBase = urlBase + newSession.urlBase = urlBase } if let user { - nkSession.user = user + newSession.user = user } if let userId { - nkSession.userId = userId + newSession.userId = userId } if let password { - nkSession.password = password + newSession.password = password } if let userAgent { - nkSession.userAgent = userAgent - } - if let replaceWithAccount { - nkSession.account = replaceWithAccount + newSession.userAgent = userAgent } + nkCommonInstance.nksessions.replace(account: account, with: newSession) } public func deleteCookieStorageForAccount(_ account: String) { diff --git a/Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift b/Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift index a2d84055..bc0a2c47 100644 --- a/Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift +++ b/Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift @@ -75,6 +75,23 @@ public final class SynchronizedNKSessionArray: @unchecked Sendable { } } + /// Replaces the first stored session that matches the given account identifier with a new session. + /// + /// This method performs the replacement in a thread-safe manner using a barrier write + /// on the internal concurrent queue. If no session with the specified account is found, + /// the array remains unchanged. + /// + /// - Parameters: + /// - account: The account identifier of the session to replace. + /// - newSession: The `NKSession` instance that will replace the existing one. + public func replace(account: String, with newSession: NKSession) { + queue.async(flags: .barrier) { + if let idx = self.array.firstIndex(where: { $0.account == account }) { + self.array[idx] = newSession + } + } + } + // MARK: - Write Operations /// Appends a new session to the array.