Rework flush of registration connection(s).

pull/1/head
Matthew Chen 7 years ago
parent 5bbce14020
commit e1138df771

@ -132,36 +132,22 @@ void runAsyncRegistrationsForStorage(OWSStorage *storage)
{ {
OWSAssert(completion); OWSAssert(completion);
[((OWSDatabase *)self.database)collectRegistrationConnections];
runAsyncRegistrationsForStorage(self); runAsyncRegistrationsForStorage(self);
DDLogVerbose(@"%@ async registrations enqueued.", self.logTag); DDLogVerbose(@"%@ async registrations enqueued.", self.logTag);
// Flush the write queue to ensure all async registrations have begun. // Flush the write queue to ensure all async registrations have begun.
[[self registrationConnection] [[self newDatabaseConnection]
flushTransactionsWithCompletionQueue:dispatch_get_main_queue() flushTransactionsWithCompletionQueue:dispatch_get_main_queue()
completionBlock:^{ completionBlock:^{
// Block until all async registrations are complete. // Block until all async registrations are complete.
// //
// NOTE: This has to happen on the "registration connections" for this // NOTE: This has to happen on the "registration connections" for this
// database. // database.
NSMutableSet<YapDatabaseConnection *> *pendingRegistrationConnectionSet = [
[((OWSDatabase *)self.database)clearCollectedRegistrationConnections] mutableCopy];
DDLogVerbose(@"%@ flushing registration connections: %zd.",
self.logTag,
pendingRegistrationConnectionSet.count);
dispatch_async(dispatch_get_main_queue(), ^{
for (YapDatabaseConnection *dbConnection in pendingRegistrationConnectionSet) {
[dbConnection
flushTransactionsWithCompletionQueue:dispatch_get_main_queue()
completionBlock:^{
OWSAssertIsOnMainThread();
OWSAssert(!self.areAsyncRegistrationsComplete); OWSAssert(!self.areAsyncRegistrationsComplete);
[pendingRegistrationConnectionSet [pendingRegistrationConnectionSet removeObject:dbConnection];
removeObject:dbConnection];
if (pendingRegistrationConnectionSet.count > 0) { if (pendingRegistrationConnectionSet.count > 0) {
DDLogVerbose(@"%@ registration " DDLogVerbose(@"%@ registration "
@"connection flushed.", @"connection flushed.",
@ -169,17 +155,13 @@ void runAsyncRegistrationsForStorage(OWSStorage *storage)
return; return;
} }
DDLogVerbose(@"%@ async registrations complete.", DDLogVerbose(@"%@ async registrations complete.", self.logTag);
self.logTag);
self.areAsyncRegistrationsComplete = YES; self.areAsyncRegistrationsComplete = YES;
completion(); completion();
}]; }];
} }
});
}];
}
+ (void)protectFiles + (void)protectFiles
{ {

@ -44,12 +44,6 @@ extern NSString *const StorageIsReadyNotification;
options:(YapDatabaseOptions *)inOptions options:(YapDatabaseOptions *)inOptions
delegate:(id<OWSDatabaseConnectionDelegate>)delegate NS_DESIGNATED_INITIALIZER; delegate:(id<OWSDatabaseConnectionDelegate>)delegate NS_DESIGNATED_INITIALIZER;
// Starts collecting references to the registration connections.
- (void)collectRegistrationConnections;
// Stops collecting references to the registration connections and returns
// all collected connections.
- (NSSet<YapDatabaseConnection *> *)clearCollectedRegistrationConnections;
@end @end
#pragma mark - #pragma mark -

@ -134,8 +134,6 @@ typedef NSData *_Nullable (^CreateDatabaseMetadataBlock)(void);
@property (atomic, weak) id<OWSDatabaseConnectionDelegate> delegate; @property (atomic, weak) id<OWSDatabaseConnectionDelegate> delegate;
@property (nonatomic, readonly, nullable) NSMutableSet<YapDatabaseConnection *> *registrationConnectionSet;
@end @end
#pragma mark - #pragma mark -
@ -185,29 +183,9 @@ typedef NSData *_Nullable (^CreateDatabaseMetadataBlock)(void);
((OWSDatabaseConnection *)connection).canWriteBeforeStorageReady = YES; ((OWSDatabaseConnection *)connection).canWriteBeforeStorageReady = YES;
#endif #endif
[self.registrationConnectionSet addObject:connection];
return connection; return connection;
} }
- (void)collectRegistrationConnections
{
OWSAssert(!self.registrationConnectionSet);
_registrationConnectionSet = [NSMutableSet set];
}
- (NSSet<YapDatabaseConnection *> *)clearCollectedRegistrationConnections
{
OWSAssert(self.registrationConnectionSet);
NSSet<YapDatabaseConnection *> *registrationConnectionSetCopy = [self.registrationConnectionSet copy];
_registrationConnectionSet = nil;
return registrationConnectionSetCopy;
}
@end @end
#pragma mark - #pragma mark -

Loading…
Cancel
Save