Switch from a single bool to a bitset to track database loading.
Switch from a single bool to a bitset to track database loading.
diff --git a/Source/DataModel.h b/Source/DataModel.h
index 2d29c87..11926d3 100644
--- a/Source/DataModel.h
+++ b/Source/DataModel.h
@@ -7,6 +7,7 @@
// Foundation, either version 3 of the License, or any later version.
//
+#include <bitset>
#import <Cocoa/Cocoa.h>
#import "LocateDatabaseOperation.h"
@@ -14,6 +15,13 @@
@protocol DataModelDelegate;
@class ThreadSafeDelegate;
+enum RequiredDatabases {
+ kSMSDatabaseModel = 0,
+ kGroupedMessagesDatabaseModel,
+ kAddressBookDatabaseModel,
+ kDatabaseModelCount
+};
+
@interface DataModel : NSObject<LocateDatabaseOperationDelegate> {
@private
// The path to the backup directory.
@@ -28,9 +36,12 @@
__strong ThreadSafeDelegate<DataModelDelegate>* delegate_;
// The lock and used when loading people from the address book.
- BOOL groupedMessagesLoaded_;
__strong NSCondition* peopleCondition_;
+ // A bitset reprsenting the databases of the RequiredDatabases enum that have
+ // been loaded.
+ std::bitset<kDatabaseModelCount> loadedDatabases_;
+
// The dictionary of unique message IDs to the Message record.
__strong NSDictionary* messageStorage_;
diff --git a/Source/DataModel.mm b/Source/DataModel.mm
index 9221cf2..814f3bd 100644
--- a/Source/DataModel.mm
+++ b/Source/DataModel.mm
@@ -34,6 +34,7 @@
protocol:@protocol(DataModelDelegate)
thread:[NSThread mainThread]];
peopleCondition_ = [[NSCondition alloc] init];
+ loadedDatabases_.reset();
[self performSelectorInBackground:@selector(locateDatabases)
withObject:nil];
}
@@ -124,7 +125,8 @@
forKey:[row objectForKey:@"ROWID"]];
}
messageStorage_ = [messages retain];
-
+
+ loadedDatabases_.set(kSMSDatabaseModel);
[delegate_ messagesLoaded];
NSMutableDictionary* buckets = [[NSMutableDictionary alloc] init];
@@ -146,7 +148,7 @@
}
[peopleCondition_ lock];
- groupedMessagesLoaded_ = YES;
+ loadedDatabases_.set(kGroupedMessagesDatabaseModel);
groupedMessages_ = buckets;
[peopleCondition_ signal];
[peopleCondition_ unlock];
@@ -194,10 +196,10 @@
// At this point, we need to have all the messages grouped by phone number,
// so we can attach names to the PhoneNumber records.
[peopleCondition_ lock];
- if (!groupedMessagesLoaded_)
+ if (!loadedDatabases_.test(kGroupedMessagesDatabaseModel))
[peopleCondition_ wait];
[peopleCondition_ unlock];
- assert(groupedMessagesLoaded_);
+ assert(loadedDatabases_.test(kGroupedMessagesDatabaseModel));
// Now go through all the phone numbers, attaching names to them.
RowReader numberReader(handle);
@@ -214,6 +216,7 @@
record.name = name;
}
+ loadedDatabases_.set(kAddressBookDatabaseModel);
[delegate_ addressBookLoaded];
}