From 3c4a19e30dec7f020d39a4cf01412811f167ac65 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 13 Sep 2021 14:37:10 +1000 Subject: [PATCH] fix nested transaction crash --- Session/Notifications/AppNotifications.swift | 2 +- SessionMessagingKit/Database/Storage+Contacts.swift | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index ba078de92..fbb44d372 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -174,7 +174,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { } let context = Contact.context(for: thread) - let senderName = Storage.shared.getContact(with: incomingMessage.authorId)?.displayName(for: context) ?? incomingMessage.authorId + let senderName = Storage.shared.getContact(with: incomingMessage.authorId, using: transaction)?.displayName(for: context) ?? incomingMessage.authorId let notificationTitle: String? switch previewType { diff --git a/SessionMessagingKit/Database/Storage+Contacts.swift b/SessionMessagingKit/Database/Storage+Contacts.swift index 04f3cf128..5581de050 100644 --- a/SessionMessagingKit/Database/Storage+Contacts.swift +++ b/SessionMessagingKit/Database/Storage+Contacts.swift @@ -7,8 +7,16 @@ extension Storage { public func getContact(with sessionID: String) -> Contact? { var result: Contact? Storage.read { transaction in - result = transaction.object(forKey: sessionID, inCollection: Storage.contactCollection) as? Contact + result = self.getContact(with: sessionID, using: transaction) } + return result + } + + @objc(getContactWithSessionID:using:) + public func getContact(with sessionID: String, using transaction: Any) -> Contact? { + var result: Contact? + let transaction = transaction as! YapDatabaseReadTransaction + result = transaction.object(forKey: sessionID, inCollection: Storage.contactCollection) as? Contact if let result = result, result.sessionID == getUserHexEncodedPublicKey() { result.isTrusted = true // Always trust ourselves }