damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 89bb293acd2480e1f670ed2625452e0ed8773e6c
parent f9c330aebf9167158cc6938e7ba85410b185d972
Author: Bryan Montz <bryanmontz@me.com>
Date:   Sun,  5 Mar 2023 07:28:51 -0600

Prune EventCache when iOS fires memory warning

Closes: #736

Diffstat:
Mdamus/Util/EventCache.swift | 18++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -5,17 +5,23 @@ // Created by William Casarin on 2023-02-21. // +import Combine import Foundation +import UIKit class EventCache { - private var events: [String: NostrEvent] - private var replies: ReplyMap = ReplyMap() + private var events: [String: NostrEvent] = [:] + private var replies = ReplyMap() + private var cancellable: AnyCancellable? //private var thread_latest: [String: Int64] init() { - self.events = [:] - self.replies = ReplyMap() + cancellable = NotificationCenter.default.publisher( + for: UIApplication.didReceiveMemoryWarningNotification + ).sink { [weak self] _ in + self?.prune() + } } func parent_events(event: NostrEvent) -> [NostrEvent] { @@ -79,4 +85,8 @@ class EventCache { events[ev.id] = ev } + private func prune() { + events = [:] + replies.replies = [:] + } }