commit ce599badee585b9535075f790021bf407f56e83a
parent 221fffe523f4c0e1e67c9b7bae83b84f8991443f
Author: Bryan Montz <bryanmontz@me.com>
Date: Tue, 9 May 2023 23:39:41 -0500
fix crash related to preloading events
Changelog-Fixed: fix crash related to preloading events
Diffstat:
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/damus/Nostr/Profiles.swift b/damus/Nostr/Profiles.swift
@@ -10,6 +10,13 @@ import UIKit
class Profiles {
+
+ /// This queue is used to synchronize access to the profiles dictionary, which
+ /// prevents data races from crashing the app.
+ private var queue = DispatchQueue(label: "io.damus.profiles",
+ qos: .userInteractive,
+ attributes: .concurrent)
+
var profiles: [String: TimestampedProfile] = [:]
var validated: [String: NIP05] = [:]
var nip05_pubkey: [String: String] = [:]
@@ -28,14 +35,20 @@ class Profiles {
}
func add(id: String, profile: TimestampedProfile) {
- profiles[id] = profile
+ queue.async(flags: .barrier) {
+ self.profiles[id] = profile
+ }
}
func lookup(id: String) -> Profile? {
- return profiles[id]?.profile
+ queue.sync {
+ return profiles[id]?.profile
+ }
}
func lookup_with_timestamp(id: String) -> TimestampedProfile? {
- return profiles[id]
+ queue.sync {
+ return profiles[id]
+ }
}
}