Skip to content

Commit c9f091e

Browse files
authored
Merge pull request #2121 from Eitot/feature/database-initialisation
Change Database class initialisation process Avoid calling most of the database setup code from within the `dispatch_once` block of the singleton initialiser: prevent objects that are meant to be accessed on the main thread from being accessed from within that block. So, most of the database setup has been moved into a `‑loadDatabaseStore` method that is called from `‑applicationDidFinishLaunching:`
2 parents bee2eb9 + 0697687 commit c9f091e

6 files changed

Lines changed: 251 additions & 152 deletions

File tree

Vienna/Sources/Application/AppController.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ - (void)installScriptsFolderWatcher {
230230
-(void)applicationDidFinishLaunching:(NSNotification *)aNot
231231
{
232232
// Initialize the database
233-
if ((db = [Database sharedManager]) == nil) {
233+
db = Database.sharedManager;
234+
if (!db || ![db loadDatabaseStore]) {
234235
[NSApp terminate:nil];
235236
return;
236237
}
@@ -954,7 +955,7 @@ -(void)initSortMenu
954955
NSMenu * sortSubmenu = [NSMenu new];
955956

956957
// Add the fields which are sortable to the menu.
957-
for (Field * field in [db arrayOfFields]) {
958+
for (Field *field in db.fields) {
958959
if (field.customizationOptions & VNAFieldCustomizationSorting) {
959960
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:field.displayName
960961
action:@selector(changeSortColumn:)
@@ -985,7 +986,7 @@ -(void)initSortMenu
985986
-(void)initColumnsMenu
986987
{
987988
NSMenu * columnsSubMenu = [NSMenu new];
988-
for (Field * field in [db arrayOfFields]) {
989+
for (Field *field in db.fields) {
989990
if (field.customizationOptions & VNAFieldCustomizationVisibility) {
990991
NSMenuItem *menuItem =
991992
[[NSMenuItem alloc] initWithTitle:field.displayName

Vienna/Sources/Database/Database.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ extern NSNotificationName const VNADatabaseDidDeleteFolderNotification;
4141

4242
@property (class, readonly, nonatomic) Database *sharedManager NS_SWIFT_NAME(shared);
4343

44+
/**
45+
Loads the database store.
46+
47+
This method performs these operations in order:
48+
1. Retrieve and create if necessary the database path.
49+
2. Load the database queue and ask if necessary to relocate the database.
50+
3. Perform a SQLite3 `quick_check` and alert if issues are found.
51+
4. Depending on the database version:
52+
- perform a database migration,
53+
- alert that the database is too old or
54+
- set up an initial database.
55+
@return `YES` if the database is loaded and has the current version or `NO`
56+
if any of the operations failed.
57+
*/
58+
- (BOOL)loadDatabaseStore;
59+
4460
@property(nonatomic) Folder * trashFolder;
4561
@property(nonatomic) Folder * searchFolder;
4662
@property (copy, nonatomic) NSString *searchString;
@@ -55,8 +71,7 @@ extern NSNotificationName const VNADatabaseDidDeleteFolderNotification;
5571
-(void)close;
5672

5773
// Fields functions
58-
-(void)addField:(NSString *)name type:(NSInteger)type sqlField:(NSString *)sqlField visible:(BOOL)visible width:(NSInteger)width;
59-
-(NSArray *)arrayOfFields;
74+
@property (readonly, nonatomic) NSArray<Field *> *fields;
6075
-(Field *)fieldByName:(NSString *)name;
6176

6277
// Folder functions

0 commit comments

Comments
 (0)