From 0a57e7db096867f8c6a00358915f5367a5bf13c0 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 22 Aug 2017 16:48:17 -0400 Subject: [PATCH] Fix slow start crash after upgrade to 2.16 We were opening a write transaction before our sync extensions were registered. This seems to have caused our views to rebuild themselves once they did register, which in turn can cause device timeout. Instead of opening transcations in `init`, we only build the localProfile once it's needed. A future PR will ensure transactions aren't being created before syncViews are registered. // FREEBIE --- Signal/src/Profiles/OWSProfileManager.m | 42 ++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index 8c02c0cab..263a49d6d 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -106,7 +106,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; @property (nonatomic, readonly) TSNetworkManager *networkManager; // This property can be accessed on any thread, while synchronized on self. -@property (atomic, nullable) UserProfile *localUserProfile; +@property (nonatomic, readonly) UserProfile *localUserProfile; // This property can be accessed on any thread, while synchronized on self. @property (atomic, nullable) UIImage *localCachedAvatarImage; @@ -130,6 +130,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; // Writes should happen off the main thread, wherever possible. @implementation OWSProfileManager +@synthesize localUserProfile = _localUserProfile; + + (instancetype)sharedManager { static OWSProfileManager *sharedMyManager = nil; @@ -175,20 +177,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; OWSSingletonAssert(); - self.localUserProfile = [self getOrBuildUserProfileForRecipientId:kLocalProfileUniqueId]; - OWSAssert(self.localUserProfile); - if (!self.localUserProfile.profileKey) { - DDLogInfo(@"%@ Generating local profile key", self.tag); - self.localUserProfile.profileKey = [OWSAES256Key generateRandomKey]; - // Make sure to save on the local db connection for consistency. - // - // NOTE: we do an async read/write here to avoid blocking during app launch path. - [self.dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - [self.localUserProfile saveWithTransaction:transaction]; - }]; - } - OWSAssert(self.localUserProfile.profileKey.keyData.length == kAES256_KeyByteLength); - return self; } @@ -264,6 +252,30 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; #pragma mark - Local Profile +- (UserProfile *)localUserProfile +{ + @synchronized(self) + { + if (_localUserProfile == nil) { + // Make sure to read on the local db connection for consistency. + [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + _localUserProfile = [UserProfile fetchObjectWithUniqueID:kLocalProfileUniqueId transaction:transaction]; + }]; + + if (_localUserProfile == nil) { + DDLogInfo(@"%@ Building local profile.", self.tag); + _localUserProfile = [[UserProfile alloc] initWithRecipientId:kLocalProfileUniqueId]; + _localUserProfile.profileKey = [OWSAES256Key generateRandomKey]; + [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + [_localUserProfile saveWithTransaction:transaction]; + }]; + } + } + + return _localUserProfile; + } +} + - (OWSAES256Key *)localProfileKey { @synchronized(self)