Skip to content

Commit 54d135c

Browse files
marinofaggianampivchev
authored andcommitted
fix
1 parent e0b12e9 commit 54d135c

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

Sources/NextcloudKit/NextcloudKit.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,45 @@ open class NextcloudKit {
108108
nkCommonInstance.nksessions.append(nkSession)
109109
}
110110

111+
/// Updates an existing `NKSession` stored in the synchronized array.
112+
///
113+
/// This method looks up the session by its `account` identifier, applies any non-nil
114+
/// parameters to mutate the session, and then replaces the stored value using
115+
/// `SynchronizedNKSessionArray.replace(account:with:)`.
116+
///
117+
/// - Parameters:
118+
/// - account: The account identifier used to locate the session to update.
119+
/// - urlBase: An optional new base URL for the session.
120+
/// - user: An optional new username for the session.
121+
/// - userId: An optional new user identifier for the session.
122+
/// - password: An optional new password or token for the session.
123+
/// - userAgent: An optional new User-Agent string for the session.
111124
public func updateSession(account: String,
112125
urlBase: String? = nil,
113126
user: String? = nil,
114127
userId: String? = nil,
115128
password: String? = nil,
116-
userAgent: String? = nil,
117-
replaceWithAccount: String? = nil) {
118-
guard var nkSession = nkCommonInstance.nksessions.session(forAccount: account) else {
129+
userAgent: String? = nil) {
130+
guard var newSession = nkCommonInstance.nksessions.session(forAccount: account) else {
119131
return
120132
}
121133

122134
if let urlBase {
123-
nkSession.urlBase = urlBase
135+
newSession.urlBase = urlBase
124136
}
125137
if let user {
126-
nkSession.user = user
138+
newSession.user = user
127139
}
128140
if let userId {
129-
nkSession.userId = userId
141+
newSession.userId = userId
130142
}
131143
if let password {
132-
nkSession.password = password
144+
newSession.password = password
133145
}
134146
if let userAgent {
135-
nkSession.userAgent = userAgent
136-
}
137-
if let replaceWithAccount {
138-
nkSession.account = replaceWithAccount
147+
newSession.userAgent = userAgent
139148
}
149+
nkCommonInstance.nksessions.replace(account: account, with: newSession)
140150
}
141151

142152
public func deleteCookieStorageForAccount(_ account: String) {

Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ public final class SynchronizedNKSessionArray: @unchecked Sendable {
7575
}
7676
}
7777

78+
/// Replaces the first stored session that matches the given account identifier with a new session.
79+
///
80+
/// This method performs the replacement in a thread-safe manner using a barrier write
81+
/// on the internal concurrent queue. If no session with the specified account is found,
82+
/// the array remains unchanged.
83+
///
84+
/// - Parameters:
85+
/// - account: The account identifier of the session to replace.
86+
/// - newSession: The `NKSession` instance that will replace the existing one.
87+
public func replace(account: String, with newSession: NKSession) {
88+
queue.async(flags: .barrier) {
89+
if let idx = self.array.firstIndex(where: { $0.account == account }) {
90+
self.array[idx] = newSession
91+
}
92+
}
93+
}
94+
7895
// MARK: - Write Operations
7996

8097
/// Appends a new session to the array.

0 commit comments

Comments
 (0)