|
1 | | -// Copyright © 2020-2021 ObjectBox. All rights reserved. |
| 1 | +// Copyright © 2020-2025 ObjectBox. All rights reserved. |
2 | 2 |
|
3 | 3 | import Foundation |
4 | 4 |
|
@@ -76,15 +76,15 @@ class SyncClientImpl: SyncClient { |
76 | 76 |
|
77 | 77 |
|
78 | 78 | public var updateRequestMode: RequestUpdatesMode { |
| 79 | + get { |
| 80 | + return updateRequestModeStorage |
| 81 | + } |
79 | 82 | set(newValue) { |
80 | 83 | let cMode = OBXRequestUpdatesMode(newValue.rawValue) |
81 | 84 | if cSync != nil && obx_sync_request_updates_mode(cSync, cMode) == OBX_SUCCESS { |
82 | 85 | updateRequestModeStorage = newValue |
83 | 86 | } |
84 | 87 | } |
85 | | - get { |
86 | | - return updateRequestModeStorage |
87 | | - } |
88 | 88 | } |
89 | 89 |
|
90 | 90 | init(store: Store, server: URL) throws { |
@@ -148,15 +148,70 @@ class SyncClientImpl: SyncClient { |
148 | 148 | } |
149 | 149 | } |
150 | 150 |
|
| 151 | + public func putFilterVariable(name: String, value: String) throws { |
| 152 | + try ensureValid() |
| 153 | + try checkLastError(obx_sync_filter_variables_put(cSync, name, value)) |
| 154 | + } |
| 155 | + |
| 156 | + public func removeFilterVariable(_ name: String) throws { |
| 157 | + try ensureValid() |
| 158 | + try checkLastError(obx_sync_filter_variables_remove(cSync, name)) |
| 159 | + } |
| 160 | + |
| 161 | + public func removeAllFilterVariables() throws { |
| 162 | + try ensureValid() |
| 163 | + try checkLastError(obx_sync_filter_variables_remove_all(cSync)) |
| 164 | + } |
| 165 | + |
151 | 166 | public func setCredentials(_ credentials: SyncCredentials) throws { |
152 | 167 | try ensureValid() |
153 | 168 | // Note: we don't store credentials in Swift memory for security reasons |
154 | | - let credentialsLength = credentials.data.count |
155 | | - try credentials.data.withUnsafeBytes { (rawBytes: UnsafeRawBufferPointer) -> Void in |
| 169 | + if let data = credentials.data { |
| 170 | + let credentialsLength = data.count |
| 171 | + try data.withUnsafeBytes { (rawBytes: UnsafeRawBufferPointer) in |
| 172 | + let cCredsType = OBXSyncCredentialsType(credentials.type.rawValue) |
| 173 | + obx_sync_credentials(cSync, cCredsType, rawBytes.baseAddress, credentialsLength) |
| 174 | + try checkLastError() |
| 175 | + credentialsSet = true |
| 176 | + } |
| 177 | + } else if let username = credentials.username { |
156 | 178 | let cCredsType = OBXSyncCredentialsType(credentials.type.rawValue) |
157 | | - obx_sync_credentials(cSync, cCredsType, rawBytes.baseAddress, credentialsLength) |
| 179 | + obx_sync_credentials_user_password(cSync, cCredsType, username, credentials.password!) |
158 | 180 | try checkLastError() |
159 | 181 | credentialsSet = true |
| 182 | + } else { |
| 183 | + throw ObjectBoxError.illegalArgument(message: "Credentials neither contain bytes data nor user/password") |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public func setCredentials(_ credentials: [SyncCredentials]) throws { |
| 188 | + try ensureValid() |
| 189 | + |
| 190 | + var oneCredentialSet = false |
| 191 | + |
| 192 | + for (index, credential) in credentials.enumerated() { |
| 193 | + let isLast = index + 1 == credentials.count |
| 194 | + if let data = credential.data { |
| 195 | + let credentialsLength = data.count |
| 196 | + try data.withUnsafeBytes { (rawBytes: UnsafeRawBufferPointer) in |
| 197 | + let cCredsType = OBXSyncCredentialsType(credential.type.rawValue) |
| 198 | + // TODO Need new C API |
| 199 | + obx_sync_credentials_add(cSync, cCredsType, rawBytes.baseAddress, credentialsLength, isLast) |
| 200 | + try checkLastError() |
| 201 | + oneCredentialSet = true |
| 202 | + } |
| 203 | + } else if let username = credential.username { |
| 204 | + let cCredsType = OBXSyncCredentialsType(credential.type.rawValue) |
| 205 | + obx_sync_credentials_add_user_password(cSync, cCredsType, username, credential.password!, isLast) |
| 206 | + try checkLastError() |
| 207 | + oneCredentialSet = true |
| 208 | + } else { |
| 209 | + throw ObjectBoxError.illegalArgument( |
| 210 | + message: "Credentials neither contain bytes data nor user/password") |
| 211 | + } |
| 212 | + } |
| 213 | + if oneCredentialSet { |
| 214 | + credentialsSet = true |
160 | 215 | } |
161 | 216 | } |
162 | 217 |
|
@@ -264,32 +319,32 @@ private func callWithSyncClient(_ userData: UnsafeMutableRawPointer?, action: @e |
264 | 319 | } |
265 | 320 |
|
266 | 321 | private func loginCallback(_ userData: UnsafeMutableRawPointer?) { |
267 | | - callWithSyncClient(userData, action: { (client: SyncClient) -> Void in |
| 322 | + callWithSyncClient(userData, action: { (client: SyncClient) in |
268 | 323 | client.loginListener?.loggedIn() |
269 | 324 | }) |
270 | 325 | } |
271 | 326 |
|
272 | 327 | private func loginFailureCallback(_ userData: UnsafeMutableRawPointer?, _ cCode: OBXSyncCode) { |
273 | 328 | let code = SyncCode(rawValue: cCode.rawValue)! |
274 | | - callWithSyncClient(userData, action: { (client: SyncClient) -> Void in |
| 329 | + callWithSyncClient(userData, action: { (client: SyncClient) in |
275 | 330 | client.loginListener?.loginFailed(result: code) |
276 | 331 | }) |
277 | 332 | } |
278 | 333 |
|
279 | 334 | private func connectedCallback(_ userData: UnsafeMutableRawPointer?) { |
280 | | - callWithSyncClient(userData, action: { (client: SyncClient) -> Void in |
| 335 | + callWithSyncClient(userData, action: { (client: SyncClient) in |
281 | 336 | client.connectionListener?.connected() |
282 | 337 | }) |
283 | 338 | } |
284 | 339 |
|
285 | 340 | private func disconnectedCallback(_ userData: UnsafeMutableRawPointer?) { |
286 | | - callWithSyncClient(userData, action: { (client: SyncClient) -> Void in |
| 341 | + callWithSyncClient(userData, action: { (client: SyncClient) in |
287 | 342 | client.connectionListener?.disconnected() |
288 | 343 | }) |
289 | 344 | } |
290 | 345 |
|
291 | 346 | private func updatesCompletedCallback(_ userData: UnsafeMutableRawPointer?) { |
292 | | - callWithSyncClient(userData, action: { (client: SyncClient) -> Void in |
| 347 | + callWithSyncClient(userData, action: { (client: SyncClient) in |
293 | 348 | client.completedListener?.updatesCompleted() |
294 | 349 | }) |
295 | 350 | } |
|
0 commit comments