Skip to content

Commit

Permalink
Added __weak ivar support for the database pool if you're on 10.7 or …
Browse files Browse the repository at this point in the history
…later.
  • Loading branch information
ccgus committed Aug 20, 2011
1 parent 93d4536 commit ebc0f4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
18 changes: 16 additions & 2 deletions src/FMDatabase.h
Expand Up @@ -3,6 +3,10 @@
#import "FMResultSet.h"
#import "FMDatabasePool.h"

#if MAC_OS_X_VERSION_10_7 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_5 <= __IPHONE_OS_VERSION_MAX_ALLOWED
#define FMDB_USE_WEAK_POOL 1
#endif

@interface FMDatabase : NSObject {

sqlite3* _db;
Expand All @@ -18,9 +22,16 @@

NSMutableDictionary *_cachedStatements;
NSMutableSet *_openResultSets;

#ifdef FMDB_USE_WEAK_POOL
__weak FMDatabasePool *_poolAccessViaMethodOnly;
#else
FMDatabasePool *_poolAccessViaMethodOnly;
#endif

FMDatabasePool *_pool;
NSInteger _poolPopCount;

FMDatabasePool *pool;
}


Expand All @@ -30,7 +41,6 @@
@property (assign) BOOL crashOnErrors;
@property (assign) BOOL logsErrors;
@property (retain) NSMutableDictionary *cachedStatements;
@property (assign) FMDatabasePool *pool;


+ (id)databaseWithPath:(NSString*)inPath;
Expand Down Expand Up @@ -95,6 +105,10 @@
- (FMDatabase*)popFromPool;
- (void)pushToPool;

- (FMDatabasePool *)pool;
- (void)setPool:(FMDatabasePool *)value;



@end

Expand Down
44 changes: 34 additions & 10 deletions src/FMDatabase.m
@@ -1,7 +1,6 @@
#import "FMDatabase.h"
#import "unistd.h"

#pragma message "FIXME: make _pool a weak ivar if possible."
#import <objc/runtime.h>

@interface FMDatabase ()

Expand All @@ -17,7 +16,6 @@ @implementation FMDatabase
@synthesize busyRetryTimeout=_busyRetryTimeout;
@synthesize checkedOut=_checkedOut;
@synthesize traceExecution=_traceExecution;
@synthesize pool=_pool;

+ (id)databaseWithPath:(NSString*)aPath {
return [[[self alloc] initWithPath:aPath] autorelease];
Expand Down Expand Up @@ -62,6 +60,10 @@ - (void)dealloc {
[_cachedStatements release];
[_databasePath release];

#ifdef FMDB_USE_WEAK_POOL
objc_storeWeak(&_poolAccessViaMethodOnly, nil);
#endif

[super dealloc];
}

Expand Down Expand Up @@ -944,7 +946,7 @@ - (BOOL)commit {

- (BOOL)beginDeferredTransaction {

if (_pool) {
if ([self pool]) {
[self popFromPool];
}

Expand All @@ -958,7 +960,7 @@ - (BOOL)beginDeferredTransaction {

- (BOOL)beginTransaction {

if (_pool) {
if ([self pool]) {
[self popFromPool];
}

Expand All @@ -982,7 +984,7 @@ - (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr {

NSAssert(name, @"Missing name for a savepoint", nil);

if (_pool) {
if ([self pool]) {
[self popFromPool];
}

Expand All @@ -1008,7 +1010,7 @@ - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr {
*outErr = [self lastError];
}

if (_pool) {
if ([self pool]) {
[self pushToPool];
}

Expand All @@ -1025,7 +1027,7 @@ - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr {
*outErr = [self lastError];
}

if (_pool) {
if ([self pool]) {
[self pushToPool];
}

Expand Down Expand Up @@ -1080,7 +1082,7 @@ - (void)setShouldCacheStatements:(BOOL)value {

- (FMDatabase*)popFromPool {

if (!_pool) {
if (![self pool]) {
NSLog(@"No FMDatabasePool in place for %@", self);
return 0x00;
}
Expand All @@ -1099,14 +1101,36 @@ - (void)pushToPool {
- (void)checkPoolPushBack {

if (_poolPopCount <= 0) {
[_pool pushDatabaseBackInPool:self];
[[self pool] pushDatabaseBackInPool:self];

if (_poolPopCount < 0) {
_poolPopCount = 0;
}
}
}

- (FMDatabasePool *)pool {
#ifdef FMDB_USE_WEAK_POOL
return objc_loadWeak(&_poolAccessViaMethodOnly);
#else
return _poolAccessViaMethodOnly;
#endif


}

- (void)setPool:(FMDatabasePool *)value {
#ifdef FMDB_USE_WEAK_POOL
objc_storeWeak(&_poolAccessViaMethodOnly, value);
#else
_poolAccessViaMethodOnly = value;
#endif
}






@end

Expand Down

0 comments on commit ebc0f4e

Please sign in to comment.