Prechádzať zdrojové kódy

Added __weak ivar support for the database pool if you're on 10.7 or later.

August Mueller 14 rokov pred
rodič
commit
ebc0f4edd5
2 zmenil súbory, kde vykonal 50 pridanie a 12 odobranie
  1. 16 2
      src/FMDatabase.h
  2. 34 10
      src/FMDatabase.m

+ 16 - 2
src/FMDatabase.h

@@ -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;
@@ -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;
 }
 
 
@@ -30,7 +41,6 @@
 @property (assign) BOOL crashOnErrors;
 @property (assign) BOOL logsErrors;
 @property (retain) NSMutableDictionary *cachedStatements;
-@property (assign) FMDatabasePool *pool;
 
 
 + (id)databaseWithPath:(NSString*)inPath;
@@ -95,6 +105,10 @@
 - (FMDatabase*)popFromPool;
 - (void)pushToPool;
 
+- (FMDatabasePool *)pool;
+- (void)setPool:(FMDatabasePool *)value;
+
+
 
 @end
 

+ 34 - 10
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 ()
 
@@ -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];
@@ -62,6 +60,10 @@ - (void)dealloc {
     [_cachedStatements release];
     [_databasePath release];
     
+#ifdef FMDB_USE_WEAK_POOL    
+    objc_storeWeak(&_poolAccessViaMethodOnly, nil);
+#endif
+    
     [super dealloc];
 }
 
@@ -944,7 +946,7 @@ - (BOOL)commit {
 
 - (BOOL)beginDeferredTransaction {
     
-    if (_pool) {
+    if ([self pool]) {
         [self popFromPool];
     }
     
@@ -958,7 +960,7 @@ - (BOOL)beginDeferredTransaction {
 
 - (BOOL)beginTransaction {
     
-    if (_pool) {
+    if ([self pool]) {
         [self popFromPool];
     }
     
@@ -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];
     }
     
@@ -1008,7 +1010,7 @@ - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr {
         *outErr = [self lastError];
     }
     
-    if (_pool) {
+    if ([self pool]) {
         [self pushToPool];
     }
     
@@ -1025,7 +1027,7 @@ - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr {
         *outErr = [self lastError];
     }
     
-    if (_pool) {
+    if ([self pool]) {
         [self pushToPool];
     }
     
@@ -1080,7 +1082,7 @@ - (void)setShouldCacheStatements:(BOOL)value {
 
 - (FMDatabase*)popFromPool {
     
-    if (!_pool) {
+    if (![self pool]) {
         NSLog(@"No FMDatabasePool in place for %@", self);
         return 0x00;
     }
@@ -1099,7 +1101,7 @@ - (void)pushToPool {
 - (void)checkPoolPushBack {
     
     if (_poolPopCount <= 0) {
-        [_pool pushDatabaseBackInPool:self];
+        [[self pool] pushDatabaseBackInPool:self];
         
         if (_poolPopCount < 0) {
             _poolPopCount = 0;
@@ -1107,6 +1109,28 @@ - (void)checkPoolPushBack {
     }
 }
 
+- (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