MMDatabaseConn.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // MMDatabaseConn
  3. // RichTextEditDemo
  4. //
  5. // Created by aron on 2017/5/3.
  6. // Copyright © 2017年 aron. All rights reserved.
  7. //
  8. #define DBMainThreadCheck() NSAssert([NSThread mainThread] == NO, \
  9. @"The query DB action in main thread is too slow,which may block main thread")
  10. #import "MMDatabaseConn.h"
  11. #import <sqlite3.h>
  12. #import <pthread.h>
  13. @interface MMDatabaseConn (){
  14. pthread_mutex_t _dbLock;
  15. FMDatabaseQueue *_databaseQueue;
  16. }
  17. @end
  18. @implementation MMDatabaseConn
  19. DEF_SINGLETON
  20. - (instancetype)init{
  21. self = [super init];
  22. if (self) {
  23. pthread_mutex_init(&_dbLock, NULL);
  24. }
  25. return self;
  26. }
  27. - (void)dealloc{
  28. pthread_mutex_destroy(&_dbLock);
  29. }
  30. - (void)receiveMemoryWarning{
  31. pthread_mutex_lock(&_dbLock);
  32. _databaseQueue = nil;
  33. pthread_mutex_unlock(&_dbLock);
  34. }
  35. #pragma mark - ......::::::: public :::::::......
  36. - (pthread_mutex_t*) dbLock {
  37. return &_dbLock;
  38. }
  39. #pragma mark - ......::::::: public :::::::......
  40. - (FMDatabaseQueue *)databaseQueue {
  41. pthread_mutex_lock(&_dbLock);
  42. if (_databaseQueue == nil) {
  43. _databaseQueue = [FMDatabaseQueue databaseQueueWithPath:_DBFilePath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE];
  44. }
  45. pthread_mutex_unlock(&_dbLock);
  46. return _databaseQueue;
  47. }
  48. @end