Skip to content

Commit

Permalink
Adding notification for when a YapDatabase instance is deallocated, a…
Browse files Browse the repository at this point in the history
…nd thus has closed all references to the underlying sqlite files. Fixes issue #139
  • Loading branch information
robbiehanson committed May 14, 2015
1 parent ab78f55 commit 4baf5f5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
46 changes: 42 additions & 4 deletions YapDatabase/YapDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,44 @@ typedef id __nonnull (^YapDatabaseDeserializer)(NSString *collection, NSString *
typedef id __nonnull (^YapDatabasePreSanitizer)(NSString *collection, NSString *key, id obj);
typedef void (^YapDatabasePostSanitizer)(NSString *collection, NSString *key, id obj);

/**
* This notification is posted when a YapDatabase instance is deallocated,
* and has thus closed all references to the underlying sqlite files.
*
* If you intend to delete the sqlite file(s) from disk,
* it's recommended you use this notification as a hook to do so.
*
* More info:
* The YapDatabase class itself is just a retainer for the filepath, blocks, config, etc.
* And YapDatabaseConnection(s) open a sqlite connection to the database file,
* and rely on the blocks & config in the parent YapDatabase class.
* Thus a YapDatabaseConnection instance purposely retains the YapDatabase instance.
* This means that in order to fully close all references to the underlying sqlite file(s),
* you need to deallocate YapDatabase and all associated YapDatabaseConnections.
* While this may be simple in concept, it's generally difficult to know exactly when all
* the instances have been deallocated. Especially when there may be a bunch of asynchronous operations going.
*
* Therefore the best approach is to do the following:
* - destroy your YapDatabase instance (set it to nil)
* - destroy all YapDatabaseConnection instances
* - wait for YapDatabaseClosedNotification
* - use notification as hook to delete all associated sqlite files from disk
*
* The userInfo dictionary will look like this:
* @{
* YapDatabasePathKey : <NSString of full filePath to db.sqlite file>,
* YapDatabasePathWalKey : <NSString of full filePath to db.sqlite-wal file>,
* YapDatabasePathShmKey : <NSString of full filePath to db.sqlite-shm file>,
* }
*
* This notification is always posted to the main thread.
**/
extern NSString *const YapDatabaseClosedNotification;

extern NSString *const YapDatabasePathKey;
extern NSString *const YapDatabasePathWalKey;
extern NSString *const YapDatabasePathShmKey;

/**
* This notification is posted following a readwrite transaction where the database was modified.
*
Expand All @@ -108,10 +146,10 @@ typedef void (^YapDatabasePostSanitizer)(NSString *collection, NSString *key, id
*
* The userInfo dictionary will look something like this:
* @{
* YapDatabaseSnapshotKey = <NSNumber of snapshot, incremented per read-write transaction w/modification>,
* YapDatabaseConnectionKey = <YapDatabaseConnection instance that made the modification(s)>,
* YapDatabaseExtensionsKey = <NSDictionary with individual changeset info per extension>,
* YapDatabaseCustomKey = <Optional object associated with this change, set by you>,
* YapDatabaseSnapshotKey : <NSNumber of snapshot, incremented per read-write transaction w/modification>,
* YapDatabaseConnectionKey : <YapDatabaseConnection instance that made the modification(s)>,
* YapDatabaseExtensionsKey : <NSDictionary with individual changeset info per extension>,
* YapDatabaseCustomKey : <Optional object associated with this change, set by you>,
* }
*
* This notification is always posted to the main thread.
Expand Down
21 changes: 21 additions & 0 deletions YapDatabase/YapDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
static const int ydbLogLevel = YDB_LOG_LEVEL_WARN;
#endif

NSString *const YapDatabaseClosedNotification = @"YapDatabaseClosedNotification";

NSString *const YapDatabasePathKey = @"databasePath";
NSString *const YapDatabasePathWalKey = @"databasePath_wal";
NSString *const YapDatabasePathShmKey = @"databasePath_shm";

NSString *const YapDatabaseModifiedNotification = @"YapDatabaseModifiedNotification";

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

NSDictionary *userInfo = @{
YapDatabasePathKey : self.databasePath ?: @"",
YapDatabasePathWalKey : self.databasePath_wal ?: @"",
YapDatabasePathShmKey : self.databasePath_shm ?: @""
};
NSNotification *notification =
[NSNotification notificationWithName:YapDatabaseClosedNotification
object:nil // Cannot retain self within dealloc method
userInfo:userInfo];

while ([connectionPoolValues count] > 0)
{
sqlite3 *aDb = (sqlite3 *)[[connectionPoolValues objectAtIndex:0] pointerValue];
Expand Down Expand Up @@ -583,6 +599,11 @@ - (void)dealloc
if (checkpointQueue)
dispatch_release(checkpointQueue);
#endif

dispatch_async(dispatch_get_main_queue(), ^{

[[NSNotificationCenter defaultCenter] postNotification:notification];
});
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 4baf5f5

Please sign in to comment.