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
This commit is contained in:
Michael Kirk 2017-08-22 16:48:17 -04:00
parent cda5157b9d
commit 0a57e7db09
1 changed files with 27 additions and 15 deletions

View File

@ -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)