Skip to content

Commit 4baf5f5

Browse files
committed
Adding notification for when a YapDatabase instance is deallocated, and thus has closed all references to the underlying sqlite files. Fixes issue #139
1 parent ab78f55 commit 4baf5f5

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

YapDatabase/YapDatabase.h

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,44 @@ typedef id __nonnull (^YapDatabaseDeserializer)(NSString *collection, NSString *
9797
typedef id __nonnull (^YapDatabasePreSanitizer)(NSString *collection, NSString *key, id obj);
9898
typedef void (^YapDatabasePostSanitizer)(NSString *collection, NSString *key, id obj);
9999

100+
/**
101+
* This notification is posted when a YapDatabase instance is deallocated,
102+
* and has thus closed all references to the underlying sqlite files.
103+
*
104+
* If you intend to delete the sqlite file(s) from disk,
105+
* it's recommended you use this notification as a hook to do so.
106+
*
107+
* More info:
108+
* The YapDatabase class itself is just a retainer for the filepath, blocks, config, etc.
109+
* And YapDatabaseConnection(s) open a sqlite connection to the database file,
110+
* and rely on the blocks & config in the parent YapDatabase class.
111+
* Thus a YapDatabaseConnection instance purposely retains the YapDatabase instance.
112+
* This means that in order to fully close all references to the underlying sqlite file(s),
113+
* you need to deallocate YapDatabase and all associated YapDatabaseConnections.
114+
* While this may be simple in concept, it's generally difficult to know exactly when all
115+
* the instances have been deallocated. Especially when there may be a bunch of asynchronous operations going.
116+
*
117+
* Therefore the best approach is to do the following:
118+
* - destroy your YapDatabase instance (set it to nil)
119+
* - destroy all YapDatabaseConnection instances
120+
* - wait for YapDatabaseClosedNotification
121+
* - use notification as hook to delete all associated sqlite files from disk
122+
*
123+
* The userInfo dictionary will look like this:
124+
* @{
125+
* YapDatabasePathKey : <NSString of full filePath to db.sqlite file>,
126+
* YapDatabasePathWalKey : <NSString of full filePath to db.sqlite-wal file>,
127+
* YapDatabasePathShmKey : <NSString of full filePath to db.sqlite-shm file>,
128+
* }
129+
*
130+
* This notification is always posted to the main thread.
131+
**/
132+
extern NSString *const YapDatabaseClosedNotification;
133+
134+
extern NSString *const YapDatabasePathKey;
135+
extern NSString *const YapDatabasePathWalKey;
136+
extern NSString *const YapDatabasePathShmKey;
137+
100138
/**
101139
* This notification is posted following a readwrite transaction where the database was modified.
102140
*
@@ -108,10 +146,10 @@ typedef void (^YapDatabasePostSanitizer)(NSString *collection, NSString *key, id
108146
*
109147
* The userInfo dictionary will look something like this:
110148
* @{
111-
* YapDatabaseSnapshotKey = <NSNumber of snapshot, incremented per read-write transaction w/modification>,
112-
* YapDatabaseConnectionKey = <YapDatabaseConnection instance that made the modification(s)>,
113-
* YapDatabaseExtensionsKey = <NSDictionary with individual changeset info per extension>,
114-
* YapDatabaseCustomKey = <Optional object associated with this change, set by you>,
149+
* YapDatabaseSnapshotKey : <NSNumber of snapshot, incremented per read-write transaction w/modification>,
150+
* YapDatabaseConnectionKey : <YapDatabaseConnection instance that made the modification(s)>,
151+
* YapDatabaseExtensionsKey : <NSDictionary with individual changeset info per extension>,
152+
* YapDatabaseCustomKey : <Optional object associated with this change, set by you>,
115153
* }
116154
*
117155
* This notification is always posted to the main thread.

YapDatabase/YapDatabase.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
static const int ydbLogLevel = YDB_LOG_LEVEL_WARN;
2929
#endif
3030

31+
NSString *const YapDatabaseClosedNotification = @"YapDatabaseClosedNotification";
32+
33+
NSString *const YapDatabasePathKey = @"databasePath";
34+
NSString *const YapDatabasePathWalKey = @"databasePath_wal";
35+
NSString *const YapDatabasePathShmKey = @"databasePath_shm";
36+
3137
NSString *const YapDatabaseModifiedNotification = @"YapDatabaseModifiedNotification";
3238

3339
NSString *const YapDatabaseSnapshotKey = @"snapshot";
@@ -549,6 +555,16 @@ - (void)dealloc
549555
{
550556
YDBLogVerbose(@"Dealloc <%@ %p: databaseName=%@>", [self class], self, [databasePath lastPathComponent]);
551557

558+
NSDictionary *userInfo = @{
559+
YapDatabasePathKey : self.databasePath ?: @"",
560+
YapDatabasePathWalKey : self.databasePath_wal ?: @"",
561+
YapDatabasePathShmKey : self.databasePath_shm ?: @""
562+
};
563+
NSNotification *notification =
564+
[NSNotification notificationWithName:YapDatabaseClosedNotification
565+
object:nil // Cannot retain self within dealloc method
566+
userInfo:userInfo];
567+
552568
while ([connectionPoolValues count] > 0)
553569
{
554570
sqlite3 *aDb = (sqlite3 *)[[connectionPoolValues objectAtIndex:0] pointerValue];
@@ -583,6 +599,11 @@ - (void)dealloc
583599
if (checkpointQueue)
584600
dispatch_release(checkpointQueue);
585601
#endif
602+
603+
dispatch_async(dispatch_get_main_queue(), ^{
604+
605+
[[NSNotificationCenter defaultCenter] postNotification:notification];
606+
});
586607
}
587608

588609
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)