Skip to content

Commit 2462d27

Browse files
authored
Merge pull request #58 from lixm1988/main
修复联系人模块偶现的crash
2 parents 6b4a973 + e2351b3 commit 2462d27

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

EaseChatUIKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'EaseChatUIKit'
11-
s.version = '4.15.1'
11+
s.version = '4.16.0'
1212
s.summary = 'A easy for use ChatUIKit.'
1313

1414
# This description is used to generate tags and improve search results.

Sources/EaseChatUIKit/Classes/Service/Client/ChatUIKitContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import UIKit
3232
public var chatCache: Dictionary<String,ChatUserProfileProtocol>? = Dictionary<String,ChatUserProfileProtocol>()
3333

3434
/// The cache of user information on user. Display the info on contact-list&single-chat-conversation-item&user-profile page .
35-
public var userCache: Dictionary<String,ChatUserProfileProtocol>? = Dictionary<String,ChatUserProfileProtocol>()
35+
@AtomicUnfairLock public var userCache: Dictionary<String,ChatUserProfileProtocol>? = Dictionary<String,ChatUserProfileProtocol>()
3636

3737
/// The cache of user information on group-conversation-item. The key is the user ID and the value is an object that complies with the ``ChatUserProfileProtocol`` protocol.
3838
public var groupCache: Dictionary<String,ChatUserProfileProtocol>? = Dictionary<String,ChatUserProfileProtocol>()

Sources/EaseChatUIKit/Classes/UI/Components/Contact/ViewModel/ContactViewModel.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ extension ContactViewModel: ContactListActionEventsDelegate {
288288

289289
@objc open func cacheProfiles(profiles: [ChatUserProfileProtocol]) {
290290
for profile in profiles {
291-
ChatUIKitContext.shared?.userCache?[profile.id]?.nickname = profile.nickname
292-
ChatUIKitContext.shared?.userCache?[profile.id]?.remark = profile.remark
293-
ChatUIKitContext.shared?.userCache?[profile.id]?.avatarURL = profile.avatarURL
291+
if let user = ChatUIKitContext.shared?.userCache?[profile.id] {
292+
user.nickname = profile.nickname
293+
user.remark = profile.remark
294+
user.avatarURL = profile.avatarURL
295+
}
294296
}
295297
}
296298

Sources/EaseChatUIKit/Classes/UI/Core/Foundation/PropertyWrapper.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,44 @@ import Foundation
2828
}
2929
}
3030

31+
@propertyWrapper
32+
public struct AtomicUnfairLock<T> {
33+
private var value: T
34+
private var lock = os_unfair_lock()
35+
36+
// 初始化时设置初始值
37+
public init(wrappedValue: T) {
38+
self.value = wrappedValue
39+
}
40+
41+
// 包装的属性值,自动处理加锁解锁
42+
public var wrappedValue: T {
43+
mutating get {
44+
os_unfair_lock_lock(&lock)
45+
defer { os_unfair_lock_unlock(&lock) }
46+
return value
47+
}
48+
set {
49+
os_unfair_lock_lock(&lock)
50+
defer { os_unfair_lock_unlock(&lock) }
51+
value = newValue
52+
}
53+
}
54+
55+
/// 原子性修改操作
56+
/// - Parameter transform: 对值进行修改的闭包
57+
public mutating func modify(_ transform: (inout T) -> Void) {
58+
os_unfair_lock_lock(&lock)
59+
defer { os_unfair_lock_unlock(&lock) }
60+
transform(&value)
61+
}
62+
63+
/// 原子性读取并处理值
64+
/// - Parameter transform: 处理值的闭包,返回处理结果
65+
/// - Returns: 处理结果
66+
public mutating func withValue<U>(_ transform: (T) -> U) -> U {
67+
os_unfair_lock_lock(&lock)
68+
defer { os_unfair_lock_unlock(&lock) }
69+
return transform(value)
70+
}
71+
}

0 commit comments

Comments
 (0)