Skip to content

Commit 8bc895e

Browse files
[PM-26606] Fix most strict warnings in AuthenticatorBridgeKit (#2054)
1 parent b51ba8d commit 8bc895e

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

AuthenticatorBridgeKit/AuthenticatorBridgeDataStore.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BitwardenKit
2-
import CoreData
2+
@preconcurrency import CoreData
33

44
// MARK: - AuthenticatorStoreType
55

@@ -20,8 +20,11 @@ public enum AuthenticatorBridgeStoreType {
2020
private let authenticatorBridgeModelName = "Bitwarden-Authenticator"
2121

2222
/// A data store that manages persisting data across app launches in Core Data.
23+
/// This is currently marked `@unchecked Sendable` because of how we ensure thread safety of the `backgroundContext`
24+
/// property. Once we have a minimum version of iOS 16 or higher, we can migrate to the `Synchronization` framework
25+
/// and make this more properly `Sendable`.
2326
///
24-
public class AuthenticatorBridgeDataStore {
27+
public final nonisolated class AuthenticatorBridgeDataStore: @unchecked Sendable {
2528
// MARK: Type Properties
2629

2730
/// The managed object model representing the entities in the database schema. CoreData throws
@@ -41,12 +44,29 @@ public class AuthenticatorBridgeDataStore {
4144

4245
// MARK: Properties
4346

47+
/// A thread-safe lock for `backgroundContext`. Once we have a minimum of iOS 16, we can use an
48+
/// `OSAllocatedUnfairLock` instead.
49+
private let _backgroundContextLock = DispatchQueue(label: "backgroundContext.lock")
50+
51+
/// A private backing for `backgroundContext`. The `backgroundContext` variable provides thread-safe access, and
52+
/// is what should be used. Once we have a minimum of iOS 16, this can be converted to an `OSAllocatedUnfairLock`,
53+
/// and remove the need for the additional `_backgroundContextLock`.
54+
private var _backgroundContext: NSManagedObjectContext?
55+
4456
/// A managed object context which executes on a background queue.
45-
private(set) lazy var backgroundContext: NSManagedObjectContext = {
46-
let context = persistentContainer.newBackgroundContext()
47-
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
48-
return context
49-
}()
57+
/// This is the thread-safe version of the backing variable `_backgroundContext`,
58+
/// and initializes that property lazily.
59+
public var backgroundContext: NSManagedObjectContext {
60+
_backgroundContextLock.sync {
61+
if let context = _backgroundContext {
62+
return context
63+
}
64+
let newContext = persistentContainer.newBackgroundContext()
65+
newContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
66+
_backgroundContext = newContext
67+
return newContext
68+
}
69+
}
5070

5171
/// The service used by the application to report non-fatal errors.
5272
let errorReporter: ErrorReporter

AuthenticatorBridgeKit/SharedKeychain/SharedKeychainService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public protocol SharedKeychainService: AnyObject {
2929
// MARK: - SharedKeychainServiceError
3030

3131
/// Enum with possible error cases that can be thrown from `SharedKeychainService`.
32-
public enum SharedKeychainServiceError: Error, Equatable, CustomNSError {
32+
public enum SharedKeychainServiceError: Error, Equatable, CustomNSError, Sendable {
3333
/// When a `KeychainService` is unable to locate an auth key for a given storage key.
3434
///
3535
/// - Parameter KeychainItem: The potential storage key for the auth key.

AuthenticatorBridgeKit/SharedKeychain/SharedKeychainStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44

55
/// Enumeration of support Keychain Items that can be placed in the `SharedKeychainRepository`
66
///
7-
public enum SharedKeychainItem: Equatable, Hashable {
7+
public enum SharedKeychainItem: Equatable, Hashable, Sendable {
88
/// The keychain item for the authenticator encryption key.
99
case authenticatorKey
1010

project-bwk.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ targets:
6666
base:
6767
APPLICATION_EXTENSION_API_ONLY: true
6868
INFOPLIST_FILE: AuthenticatorBridgeKit/Info.plist
69+
SWIFT_STRICT_CONCURRENCY: complete
6970
sources:
7071
- path: AuthenticatorBridgeKit
7172
excludes:
@@ -82,6 +83,7 @@ targets:
8283
settings:
8384
base:
8485
INFOPLIST_FILE: AuthenticatorBridgeKit/Tests/TestHelpers/Support/Info.plist
86+
SWIFT_STRICT_CONCURRENCY: complete
8587
sources:
8688
- path: AuthenticatorBridgeKit
8789
includes:
@@ -102,6 +104,7 @@ targets:
102104
base:
103105
ENABLE_TESTING_SEARCH_PATHS: YES
104106
INFOPLIST_FILE: AuthenticatorBridgeKit/MocksInfo.plist
107+
SWIFT_STRICT_CONCURRENCY: complete
105108
sources:
106109
- path: AuthenticatorBridgeKit
107110
includes:

0 commit comments

Comments
 (0)