Improve perf of attachment stream file path upgrade.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent ed249840c6
commit cf65cc3be5

@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
// This only applies for attachments being uploaded. // This only applies for attachments being uploaded.
@property (atomic) BOOL isUploaded; @property (atomic) BOOL isUploaded;
@property (atomic) BOOL hasUnsavedFilePath;
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
- (nullable UIImage *)image; - (nullable UIImage *)image;
#endif #endif

@ -129,29 +129,18 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(self.filePath); OWSAssert(self.filePath);
if (shouldPersist) { if (shouldPersist) {
// It's not ideal to do this asynchronously, but we can create a new transaction self.hasUnsavedFilePath = YES;
// within initWithCoder: which will be called from within a transaction.
//
// 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);
[self saveWithTransaction:transaction];
}];
});
} }
} }
+ (dispatch_queue_t)serialQueue + (MTLPropertyStorage)storageBehaviorForPropertyWithKey:(NSString *)propertyKey
{ {
static dispatch_queue_t queue = nil; // Don't persist transient properties
static dispatch_once_t onceToken; if ([propertyKey isEqualToString:@"hasUnsavedFilePath"]) {
dispatch_once(&onceToken, ^{ return MTLPropertyStorageNone;
queue = dispatch_queue_create("org.whispersystems.attachment.stream", DISPATCH_QUEUE_SERIAL); } else {
}); return [super storageBehaviorForPropertyWithKey:propertyKey];
return queue; }
} }
#pragma mark - File Management #pragma mark - File Management

Loading…
Cancel
Save