FMDatabaseQueue.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // FMDatabaseQueue.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. NS_ASSUME_NONNULL_BEGIN
  10. @class FMDatabase;
  11. /** To perform queries and updates on multiple threads, you'll want to use `FMDatabaseQueue`.
  12. Using a single instance of `<FMDatabase>` from multiple threads at once is a bad idea. It has always been OK to make a `<FMDatabase>` object *per thread*. Just don't share a single instance across threads, and definitely not across multiple threads at the same time.
  13. Instead, use `FMDatabaseQueue`. Here's how to use it:
  14. First, make your queue.
  15. FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
  16. Then use it like so:
  17. [queue inDatabase:^(FMDatabase *db) {
  18. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
  19. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
  20. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
  21. FMResultSet *rs = [db executeQuery:@"select * from foo"];
  22. while ([rs next]) {
  23. //…
  24. }
  25. }];
  26. An easy way to wrap things up in a transaction can be done like this:
  27. [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
  28. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
  29. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
  30. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
  31. if (whoopsSomethingWrongHappened) {
  32. *rollback = YES;
  33. return;
  34. }
  35. // etc…
  36. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
  37. }];
  38. `FMDatabaseQueue` will run the blocks on a serialized queue (hence the name of the class). So if you call `FMDatabaseQueue`'s methods from multiple threads at the same time, they will be executed in the order they are received. This way queries and updates won't step on each other's toes, and every one is happy.
  39. ### See also
  40. - `<FMDatabase>`
  41. @warning Do not instantiate a single `<FMDatabase>` object and use it across multiple threads. Use `FMDatabaseQueue` instead.
  42. @warning The calls to `FMDatabaseQueue`'s methods are blocking. So even though you are passing along blocks, they will **not** be run on another thread.
  43. */
  44. @interface FMDatabaseQueue : NSObject
  45. /** Path of database */
  46. @property (atomic, retain, nullable) NSString *path;
  47. /** Open flags */
  48. @property (atomic, readonly) int openFlags;
  49. /** Custom virtual file system name */
  50. @property (atomic, copy, nullable) NSString *vfsName;
  51. ///----------------------------------------------------
  52. /// @name Initialization, opening, and closing of queue
  53. ///----------------------------------------------------
  54. /** Create queue using path.
  55. @param aPath The file path of the database.
  56. @return The `FMDatabaseQueue` object. `nil` on error.
  57. */
  58. + (instancetype)databaseQueueWithPath:(NSString * _Nullable)aPath;
  59. /** Create queue using file URL.
  60. @param url The file `NSURL` of the database.
  61. @return The `FMDatabaseQueue` object. `nil` on error.
  62. */
  63. + (instancetype)databaseQueueWithURL:(NSURL * _Nullable)url;
  64. /** Create queue using path and specified flags.
  65. @param aPath The file path of the database.
  66. @param openFlags Flags passed to the openWithFlags method of the database.
  67. @return The `FMDatabaseQueue` object. `nil` on error.
  68. */
  69. + (instancetype)databaseQueueWithPath:(NSString * _Nullable)aPath flags:(int)openFlags;
  70. /** Create queue using file URL and specified flags.
  71. @param url The file `NSURL` of the database.
  72. @param openFlags Flags passed to the openWithFlags method of the database.
  73. @return The `FMDatabaseQueue` object. `nil` on error.
  74. */
  75. + (instancetype)databaseQueueWithURL:(NSURL * _Nullable)url flags:(int)openFlags;
  76. /** Create queue using path.
  77. @param aPath The file path of the database.
  78. @return The `FMDatabaseQueue` object. `nil` on error.
  79. */
  80. - (instancetype)initWithPath:(NSString * _Nullable)aPath;
  81. /** Create queue using file URL.
  82. @param url The file `NSURL of the database.
  83. @return The `FMDatabaseQueue` object. `nil` on error.
  84. */
  85. - (instancetype)initWithURL:(NSURL * _Nullable)url;
  86. /** Create queue using path and specified flags.
  87. @param aPath The file path of the database.
  88. @param openFlags Flags passed to the openWithFlags method of the database.
  89. @return The `FMDatabaseQueue` object. `nil` on error.
  90. */
  91. - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags;
  92. /** Create queue using file URL and specified flags.
  93. @param url The file path of the database.
  94. @param openFlags Flags passed to the openWithFlags method of the database.
  95. @return The `FMDatabaseQueue` object. `nil` on error.
  96. */
  97. - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags;
  98. /** Create queue using path and specified flags.
  99. @param aPath The file path of the database.
  100. @param openFlags Flags passed to the openWithFlags method of the database
  101. @param vfsName The name of a custom virtual file system
  102. @return The `FMDatabaseQueue` object. `nil` on error.
  103. */
  104. - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags vfs:(NSString * _Nullable)vfsName;
  105. /** Create queue using file URL and specified flags.
  106. @param url The file `NSURL of the database.
  107. @param openFlags Flags passed to the openWithFlags method of the database
  108. @param vfsName The name of a custom virtual file system
  109. @return The `FMDatabaseQueue` object. `nil` on error.
  110. */
  111. - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags vfs:(NSString * _Nullable)vfsName;
  112. /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  113. Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
  114. @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  115. */
  116. + (Class)databaseClass;
  117. /** Close database used by queue. */
  118. - (void)close;
  119. /** Interupt pending database operation. */
  120. - (void)interrupt;
  121. ///-----------------------------------------------
  122. /// @name Dispatching database operations to queue
  123. ///-----------------------------------------------
  124. /** Synchronously perform database operations on queue.
  125. @param block The code to be run on the queue of `FMDatabaseQueue`
  126. */
  127. - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block;
  128. /** Synchronously perform database operations on queue, using transactions.
  129. @param block The code to be run on the queue of `FMDatabaseQueue`
  130. */
  131. - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  132. /** Synchronously perform database operations on queue, using deferred transactions.
  133. @param block The code to be run on the queue of `FMDatabaseQueue`
  134. */
  135. - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  136. ///-----------------------------------------------
  137. /// @name Dispatching database operations to queue
  138. ///-----------------------------------------------
  139. /** Synchronously perform database operations using save point.
  140. @param block The code to be run on the queue of `FMDatabaseQueue`
  141. */
  142. // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock.
  143. // If you need to nest, use FMDatabase's startSavePointWithName:error: instead.
  144. - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  145. @end
  146. NS_ASSUME_NONNULL_END