Skip to content
Merged

fix #188

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions Sources/NextcloudKit/NextcloudKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions Sources/NextcloudKit/Utils/SynchronizedNKSessionArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading