From 470cee0e1108409ab8285cf13b81d830fd7e6c8f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 15 Jun 2017 17:25:25 -0400 Subject: [PATCH] Upgrade attachment streams on a serial queue. // FREEBIE --- src/Messages/Attachments/TSAttachmentStream.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Messages/Attachments/TSAttachmentStream.m b/src/Messages/Attachments/TSAttachmentStream.m index 3833c67ec..38670b800 100644 --- a/src/Messages/Attachments/TSAttachmentStream.m +++ b/src/Messages/Attachments/TSAttachmentStream.m @@ -131,7 +131,10 @@ NS_ASSUME_NONNULL_BEGIN if (shouldPersist) { // It's not ideal to do this asynchronously, but we can create a new transaction // within initWithCoder: which will be called from within a transaction. - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + // + // We use a serial queue to ensure we don't spawn a ton of threads each doing + // database writes. + dispatch_async([TSAttachmentStream serialQueue], ^{ [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { OWSAssert(transaction); @@ -141,6 +144,16 @@ NS_ASSUME_NONNULL_BEGIN } } ++ (dispatch_queue_t)serialQueue +{ + static dispatch_queue_t queue = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + queue = dispatch_queue_create("org.whispersystems.attachment.stream", DISPATCH_QUEUE_SERIAL); + }); + return queue; +} + #pragma mark - File Management - (nullable NSData *)readDataFromFileWithError:(NSError **)error