From 58fb86b8e02f92f7e7ca0ad100fbb2927ffb0a67 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 5 Jul 2017 18:54:48 -0400 Subject: [PATCH] Use a dedicated connection for model reads & writes. // FREEBIE --- src/Storage/TSYapDatabaseObject.m | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Storage/TSYapDatabaseObject.m b/src/Storage/TSYapDatabaseObject.m index ef8740157..71b71c0f5 100644 --- a/src/Storage/TSYapDatabaseObject.m +++ b/src/Storage/TSYapDatabaseObject.m @@ -80,17 +80,24 @@ + (YapDatabaseConnection *)dbReadConnection { - // We use TSStorageManager's dbReadWriteConnection (not its dbReadConnection) - // for consistency, since we tend to [TSYapDatabaseObject save] and want to - // write to the same connection we read from. To get true consistency, we'd - // want to update entities by reading & writing from within the same - // transaction, but that'll be a big refactor. - return [self storageManager].dbReadWriteConnection; + // We use TSYapDatabaseObject's dbReadWriteConnection (not TSStorageManager's + // dbReadConnection) for consistency, since we tend to [TSYapDatabaseObject + // save] and want to write to the same connection we read from. To get true + // consistency, we'd want to update entities by reading & writing from within + // the same transaction, but that'll be a big refactor. + + return self.dbReadWriteConnection; } + (YapDatabaseConnection *)dbReadWriteConnection { - return [self storageManager].dbReadWriteConnection; + // Use a dedicated connection for model reads & writes. + static YapDatabaseConnection *dbReadWriteConnection = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + dbReadWriteConnection = [self storageManager].newDatabaseConnection; + }); + return dbReadWriteConnection; } + (TSStorageManager *)storageManager