설명 없음

AWSFMDatabasePool.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // FMDatabasePool.h
  3. // fmdb
  4. //
  5. // Created by August Mueller on 6/22/11.
  6. // Copyright 2011 Flying Meat Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @class AWSFMDatabase;
  10. /** Pool of `<FMDatabase>` objects.
  11. ### See also
  12. - `<FMDatabaseQueue>`
  13. - `<FMDatabase>`
  14. @warning Before using `FMDatabasePool`, please consider using `<FMDatabaseQueue>` instead.
  15. If you really really really know what you're doing and `FMDatabasePool` is what
  16. you really really need (ie, you're using a read only database), OK you can use
  17. it. But just be careful not to deadlock!
  18. For an example on deadlocking, search for:
  19. `ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD`
  20. in the main.m file.
  21. */
  22. @interface AWSFMDatabasePool : NSObject {
  23. NSString *_path;
  24. dispatch_queue_t _lockQueue;
  25. NSMutableArray *_databaseInPool;
  26. NSMutableArray *_databaseOutPool;
  27. __unsafe_unretained id _delegate;
  28. NSUInteger _maximumNumberOfDatabasesToCreate;
  29. int _openFlags;
  30. }
  31. /** Database path */
  32. @property (atomic, retain) NSString *path;
  33. /** Delegate object */
  34. @property (atomic, assign) id delegate;
  35. /** Maximum number of databases to create */
  36. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  37. /** Open flags */
  38. @property (atomic, readonly) int openFlags;
  39. ///---------------------
  40. /// @name Initialization
  41. ///---------------------
  42. /** Create pool using path.
  43. @param aPath The file path of the database.
  44. @return The `FMDatabasePool` object. `nil` on error.
  45. */
  46. + (instancetype)databasePoolWithPath:(NSString*)aPath;
  47. /** Create pool using path and specified flags
  48. @param aPath The file path of the database.
  49. @param openFlags Flags passed to the openWithFlags method of the database
  50. @return The `FMDatabasePool` object. `nil` on error.
  51. */
  52. + (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags;
  53. /** Create pool using path.
  54. @param aPath The file path of the database.
  55. @return The `FMDatabasePool` object. `nil` on error.
  56. */
  57. - (instancetype)initWithPath:(NSString*)aPath;
  58. /** Create pool using path and specified flags.
  59. @param aPath The file path of the database.
  60. @param openFlags Flags passed to the openWithFlags method of the database
  61. @return The `FMDatabasePool` object. `nil` on error.
  62. */
  63. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
  64. ///------------------------------------------------
  65. /// @name Keeping track of checked in/out databases
  66. ///------------------------------------------------
  67. /** Number of checked-in databases in pool
  68. @returns Number of databases
  69. */
  70. - (NSUInteger)countOfCheckedInDatabases;
  71. /** Number of checked-out databases in pool
  72. @returns Number of databases
  73. */
  74. - (NSUInteger)countOfCheckedOutDatabases;
  75. /** Total number of databases in pool
  76. @returns Number of databases
  77. */
  78. - (NSUInteger)countOfOpenDatabases;
  79. /** Release all databases in pool */
  80. - (void)releaseAllDatabases;
  81. ///------------------------------------------
  82. /// @name Perform database operations in pool
  83. ///------------------------------------------
  84. /** Synchronously perform database operations in pool.
  85. @param block The code to be run on the `FMDatabasePool` pool.
  86. */
  87. - (void)inDatabase:(void (^)(AWSFMDatabase *db))block;
  88. /** Synchronously perform database operations in pool using transaction.
  89. @param block The code to be run on the `FMDatabasePool` pool.
  90. */
  91. - (void)inTransaction:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  92. /** Synchronously perform database operations in pool using deferred transaction.
  93. @param block The code to be run on the `FMDatabasePool` pool.
  94. */
  95. - (void)inDeferredTransaction:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  96. /** Synchronously perform database operations in pool using save point.
  97. @param block The code to be run on the `FMDatabasePool` pool.
  98. @return `NSError` object if error; `nil` if successful.
  99. @warning You can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock. If you need to nest, use `<[FMDatabase startSavePointWithName:error:]>` instead.
  100. */
  101. - (NSError*)inSavePoint:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  102. @end
  103. /** FMDatabasePool delegate category
  104. This is a category that defines the protocol for the FMDatabasePool delegate
  105. */
  106. @interface NSObject (AWSFMDatabasePoolDelegate)
  107. /** Asks the delegate whether database should be added to the pool.
  108. @param pool The `FMDatabasePool` object.
  109. @param database The `FMDatabase` object.
  110. @return `YES` if it should add database to pool; `NO` if not.
  111. */
  112. - (BOOL)databasePool:(AWSFMDatabasePool*)pool shouldAddDatabaseToPool:(AWSFMDatabase*)database;
  113. /** Tells the delegate that database was added to the pool.
  114. @param pool The `FMDatabasePool` object.
  115. @param database The `FMDatabase` object.
  116. */
  117. - (void)databasePool:(AWSFMDatabasePool*)pool didAddDatabase:(AWSFMDatabase*)database;
  118. @end