Skip to content

Commit

Permalink
SPPersistentMutableDictionary: Fixes mutex glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
jleandroperez committed Jul 11, 2014
1 parent e953063 commit cdae6ee
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions Simperium/SPPersistentMutableDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#pragma mark ====================================================================================

@interface SPPersistentMutableDictionary ()
@property (nonatomic, strong, readwrite) NSString *label;
@property (nonatomic, strong, readwrite) NSCache *cache;
@property (nonatomic, strong, readwrite) NSManagedObjectContext* managedObjectContext;
@property (nonatomic, strong, readwrite) NSManagedObjectModel* managedObjectModel;
@property (nonatomic, strong, readwrite) NSPersistentStoreCoordinator* persistentStoreCoordinator;
@property (nonatomic, strong, readwrite) NSString *label;
@property (nonatomic, strong, readwrite) NSCache *cache;
@property (nonatomic, strong, readwrite) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readwrite) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readwrite) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL*)baseURL;
@end

Expand All @@ -44,7 +44,8 @@ - (NSURL*)baseURL;
@implementation SPPersistentMutableDictionary

- (id)initWithLabel:(NSString *)label {
if ((self = [super init])) {
self = [super init];
if (self) {
self.label = label;
self.cache = [[NSCache alloc] init];
}
Expand All @@ -65,7 +66,6 @@ - (NSInteger)count {
}

- (BOOL)containsObjectForKey:(id)aKey {
// Failsafe
if (aKey == nil) {
return false;
}
Expand All @@ -88,7 +88,6 @@ - (BOOL)containsObjectForKey:(id)aKey {
}

- (id)objectForKey:(id)aKey {
// Failsafe
if (aKey == nil) {
return nil;
}
Expand Down Expand Up @@ -127,7 +126,6 @@ - (id)objectForKey:(id)aKey {
}

- (void)setObject:(id)anObject forKey:(NSString*)aKey {
// Failsafe
if (anObject == nil) {
[self removeObjectForKey:aKey];
return;
Expand Down Expand Up @@ -206,7 +204,6 @@ - (void)removeObjectForKey:(id)aKey {
}

- (void)removeAllObjects {
// Remove from CoreData
[self.managedObjectContext performBlock:^{

// Fetch the objectID's
Expand Down Expand Up @@ -237,43 +234,53 @@ + (instancetype)loadDictionaryWithLabel:(NSString *)label {
#pragma mark ====================================================================================

- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
if (_managedObjectModel) {
return _managedObjectModel;
}

// Dynamic Attributes
NSAttributeDescription *keyAttribute = [[NSAttributeDescription alloc] init];
[keyAttribute setName:@"key"];
[keyAttribute setAttributeType:NSStringAttributeType];
[keyAttribute setOptional:NO];
[keyAttribute setIndexed:YES];

NSAttributeDescription *valueAttribute = [[NSAttributeDescription alloc] init];
[valueAttribute setName:@"value"];
[valueAttribute setAttributeType:NSBinaryDataAttributeType];
[valueAttribute setOptional:NO];

// SPMetadata Entity
NSEntityDescription *entity = [[NSEntityDescription alloc] init];
[entity setName:SPDictionaryEntityName];
[entity setManagedObjectClassName:NSStringFromClass([NSManagedObject class])];
[entity setProperties:@[keyAttribute, valueAttribute] ];

// Done!
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] init];
[model setEntities:@[entity]];

_managedObjectModel = model;

@synchronized(self) {
if (_managedObjectModel) {
return _managedObjectModel;
}

// Dynamic Attributes
NSAttributeDescription *keyAttribute = [[NSAttributeDescription alloc] init];
keyAttribute.name = @"key";
keyAttribute.attributeType = NSStringAttributeType;
keyAttribute.optional = NO;
keyAttribute.indexed = YES;

NSAttributeDescription *valueAttribute = [[NSAttributeDescription alloc] init];
valueAttribute.name = @"value";
valueAttribute.attributeType = NSBinaryDataAttributeType;
valueAttribute.optional = NO;

// SPMetadata Entity
NSEntityDescription *entity = [[NSEntityDescription alloc] init];
entity.name = SPDictionaryEntityName;
entity.managedObjectClassName = NSStringFromClass([NSManagedObject class]);
entity.properties = @[keyAttribute, valueAttribute];

// Done!
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] init];
model.entities = @[entity];

_managedObjectModel = model;
}

return _managedObjectModel;
}

- (NSManagedObjectContext*)managedObjectContext {
if (_managedObjectContext != nil) {
if (_managedObjectContext) {
return _managedObjectContext;
}

@synchronized(self) {
if (_managedObjectContext) {
return _managedObjectContext;
}

_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
}
Expand All @@ -283,12 +290,15 @@ - (NSManagedObjectContext*)managedObjectContext {


- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}

@synchronized(self) {
// If the baseURL doesn't exist, create it
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}

NSURL *baseURL = [self baseURL];

NSError *error = nil;
Expand Down Expand Up @@ -332,8 +342,7 @@ - (NSURL *)baseURL {
}

- (void)migrateIfNeeded {

// Prepare the URL's

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *oldBaseURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *newBaseURL = self.baseURL;
Expand Down Expand Up @@ -388,8 +397,8 @@ - (NSFetchRequest *)requestForEntity {

- (NSFetchRequest *)requestForEntityWithKey:(id)aKey {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:SPDictionaryEntityName];
request.predicate = [NSPredicate predicateWithFormat:@"key == %@", aKey];
request.fetchLimit = 1;
request.predicate = [NSPredicate predicateWithFormat:@"key == %@", aKey];
request.fetchLimit = 1;

return request;
}
Expand Down

0 comments on commit cdae6ee

Please sign in to comment.