From ce599badee585b9535075f790021bf407f56e83a Mon Sep 17 00:00:00 2001 From: Bryan Montz Date: Tue, 9 May 2023 23:39:41 -0500 Subject: [PATCH] fix crash related to preloading events Changelog-Fixed: fix crash related to preloading events --- damus/Nostr/Profiles.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/damus/Nostr/Profiles.swift b/damus/Nostr/Profiles.swift index 151fe47b..fa3f06d3 100644 --- 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] + } } }