From 7fd148a91909da9937be365fb9c299ee90fbd39d Mon Sep 17 00:00:00 2001 From: Nora Trapp Date: Fri, 17 Jul 2020 12:39:36 -0700 Subject: [PATCH] Fix a crash on launch effecting some users --- SignalMessaging/profiles/OWSProfileManager.m | 26 +++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/SignalMessaging/profiles/OWSProfileManager.m b/SignalMessaging/profiles/OWSProfileManager.m index 5ff9811c24..61907f2104 100644 --- a/SignalMessaging/profiles/OWSProfileManager.m +++ b/SignalMessaging/profiles/OWSProfileManager.m @@ -259,6 +259,30 @@ const NSString *kNSNotificationKey_WasLocallyInitiated = @"kNSNotificationKey_Wa return [localUserProfile shallowCopy]; } +- (nullable OWSUserProfile *)getLocalUserProfileWithTransaction:(SDSAnyReadTransaction *)transaction +{ + @synchronized(self) { + if (_localUserProfile) { + OWSAssertDebug(_localUserProfile.profileKey); + + return [_localUserProfile shallowCopy]; + } + } + + OWSUserProfile *_Nullable localUserProfile = + [OWSUserProfile getUserProfileForAddress:OWSUserProfile.localProfileAddress transaction:transaction]; + + if (localUserProfile != nil) { + @synchronized(self) { + _localUserProfile = localUserProfile; + } + return [localUserProfile shallowCopy]; + } + + OWSFailDebug(@"We're trying to fetch the local user profile before it exists. This shouldn't happen."); + return nil; +} + - (void)localProfileWasUpdated:(OWSUserProfile *)localUserProfile { @synchronized(self) { @@ -1608,7 +1632,7 @@ const NSString *kNSNotificationKey_WasLocallyInitiated = @"kNSNotificationKey_Wa // For "local reads", use the local user profile. if ([OWSUserProfile isLocalProfileAddress:address]) { - return self.localUserProfile; + return [self getLocalUserProfileWithTransaction:transaction]; } return [self.userProfileReadCache getUserProfileWithAddress:address transaction:transaction];