Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. FMDatabase* databaseConnection(BOOL open)
  2. {
  3. static FMDatabase *connection = nil;
  4.  
  5. if(!open)
  6. {
  7. if(connection)
  8. {
  9. [connection close];
  10. [connection release];
  11. connection = nil;
  12. }
  13. return nil;
  14. }
  15.  
  16. if(connection)
  17. {
  18. return connection;
  19. }
  20.  
  21. NSString *storePath = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Data.db"];
  22. NSFileManager *fileManager = [NSFileManager defaultManager];
  23. // If the expected store doesn't exist, copy the default store.
  24. if (![fileManager fileExistsAtPath:storePath])
  25. {
  26. NSLog(@"Data.db file not exists: copying the default one.");
  27. NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"db"];
  28. if (defaultStorePath)
  29. {
  30. [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
  31. }
  32. }
  33.  
  34. connection = [[FMDatabase databaseWithPath:storePath] retain];
  35. [connection open];
  36. return connection;
  37. }
  38.  
  39. NSString* applicationDocumentsDirectory()
  40. {
  41. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  42. NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  43. return basePath;
  44. }
Add Comment
Please, Sign In to add comment