Ingen beskrivning

AWSFMDatabaseQueue.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. @class AWSFMDatabase;
  10. /** To perform queries and updates on multiple threads, you'll want to use `FMDatabaseQueue`.
  11. 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.
  12. Instead, use `FMDatabaseQueue`. Here's how to use it:
  13. First, make your queue.
  14. FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
  15. Then use it like so:
  16. [queue inDatabase:^(FMDatabase *db) {
  17. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
  18. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
  19. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
  20. FMResultSet *rs = [db executeQuery:@"select * from foo"];
  21. while ([rs next]) {
  22. //…
  23. }
  24. }];
  25. An easy way to wrap things up in a transaction can be done like this:
  26. [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
  27. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
  28. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
  29. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
  30. if (whoopsSomethingWrongHappened) {
  31. *rollback = YES;
  32. return;
  33. }
  34. // etc…
  35. [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
  36. }];
  37. `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.
  38. ### See also
  39. - `<FMDatabase>`
  40. @warning Do not instantiate a single `<FMDatabase>` object and use it across multiple threads. Use `FMDatabaseQueue` instead.
  41. @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.
  42. */
  43. @interface AWSFMDatabaseQueue : NSObject {
  44. NSString *_path;
  45. dispatch_queue_t _queue;
  46. AWSFMDatabase *_db;
  47. int _openFlags;
  48. }
  49. /** Path of database */
  50. @property (atomic, retain) NSString *path;
  51. /** Open flags */
  52. @property (atomic, readonly) int openFlags;
  53. ///----------------------------------------------------
  54. /// @name Initialization, opening, and closing of queue
  55. ///----------------------------------------------------
  56. /** Create queue using path.
  57. @param aPath The file path of the database.
  58. @return The `FMDatabaseQueue` object. `nil` on error.
  59. */
  60. + (instancetype)databaseQueueWithPath:(NSString*)aPath;
  61. /** Create queue using path and specified flags.
  62. @param aPath The file path of the database.
  63. @param openFlags Flags passed to the openWithFlags method of the database
  64. @return The `FMDatabaseQueue` object. `nil` on error.
  65. */
  66. + (instancetype)databaseQueueWithPath:(NSString*)aPath flags:(int)openFlags;
  67. /** Create queue using path.
  68. @param aPath The file path of the database.
  69. @return The `FMDatabaseQueue` object. `nil` on error.
  70. */
  71. - (instancetype)initWithPath:(NSString*)aPath;
  72. /** Create queue using path and specified flags.
  73. @param aPath The file path of the database.
  74. @param openFlags Flags passed to the openWithFlags method of the database
  75. @return The `FMDatabaseQueue` object. `nil` on error.
  76. */
  77. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
  78. /** Create queue using path and specified flags.
  79. @param aPath The file path of the database.
  80. @param openFlags Flags passed to the openWithFlags method of the database
  81. @param vfsName The name of a custom virtual file system
  82. @return The `FMDatabaseQueue` object. `nil` on error.
  83. */
  84. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName;
  85. /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  86. Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
  87. @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  88. */
  89. + (Class)databaseClass;
  90. /** Close database used by queue. */
  91. - (void)close;
  92. ///-----------------------------------------------
  93. /// @name Dispatching database operations to queue
  94. ///-----------------------------------------------
  95. /** Synchronously perform database operations on queue.
  96. @param block The code to be run on the queue of `FMDatabaseQueue`
  97. */
  98. - (void)inDatabase:(void (^)(AWSFMDatabase *db))block;
  99. /** Synchronously perform database operations on queue, using transactions.
  100. @param block The code to be run on the queue of `FMDatabaseQueue`
  101. */
  102. - (void)inTransaction:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  103. /** Synchronously perform database operations on queue, using deferred transactions.
  104. @param block The code to be run on the queue of `FMDatabaseQueue`
  105. */
  106. - (void)inDeferredTransaction:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  107. ///-----------------------------------------------
  108. /// @name Dispatching database operations to queue
  109. ///-----------------------------------------------
  110. /** Synchronously perform database operations using save point.
  111. @param block The code to be run on the queue of `FMDatabaseQueue`
  112. */
  113. // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock.
  114. // If you need to nest, use FMDatabase's startSavePointWithName:error: instead.
  115. - (NSError*)inSavePoint:(void (^)(AWSFMDatabase *db, BOOL *rollback))block;
  116. @end