Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to version 2.3.5

  • Loading branch information...
commit 92493bcb27ec609571c78c130caa37f6cc0a2ff2 1 parent cbc9b74
Nick Lockwood authored
2  BaseModel/BaseModel.h
View
@@ -1,7 +1,7 @@
//
// BaseModel.h
//
-// Version 2.3.4
+// Version 2.3.5
//
// Created by Nick Lockwood on 25/06/2011.
// Copyright 2011 Charcoal Design
74 BaseModel/BaseModel.m
View
@@ -1,7 +1,7 @@
//
// BaseModel.m
//
-// Version 2.3.4
+// Version 2.3.5
//
// Created by Nick Lockwood on 25/06/2011.
// Copyright 2011 Charcoal Design
@@ -62,11 +62,16 @@
#import "BaseModel.h"
#import <objc/message.h>
+#import <objc/runtime.h>
NSString *const BaseModelSharedInstanceUpdatedNotification = @"BaseModelSharedInstanceUpdatedNotification";
+static NSString *const BaseModelSharedInstanceKey = @"sharedInstance";
+static NSString *const BaseModelLoadingFromResourceFileKey = @"loadingFromResourceFile";
+
+
@implementation BaseModel
#pragma mark -
@@ -122,28 +127,49 @@ + (NSString *)saveFilePath
return [self saveFilePath:[self saveFile]];
}
-#pragma mark -
-#pragma mark Singleton behaviour
+static NSMutableDictionary *classValues = nil;
-static NSMutableDictionary *sharedInstances = nil;
++ (id)classPropertyForKey:(NSString *)key
+{
+ NSString *className = NSStringFromClass(self);
+ return [[classValues objectForKey:className] objectForKey:key];
+}
-+ (void)setSharedInstance:(BaseModel *)instance
++ (void)setClassProperty:(id)property forKey:(NSString *)key
{
- if (instance && ![instance isKindOfClass:self])
+ NSString *className = NSStringFromClass(self);
+ if (!classValues)
{
- [NSException raise:NSGenericException format:@"setSharedInstance: instance class does not match"];
+ classValues = [NSMutableDictionary dictionary];
+ }
+ NSMutableDictionary *values = [classValues objectForKey:className];
+ if (!values)
+ {
+ values = [NSMutableDictionary dictionary];
+ [classValues setObject:values forKey:className];
}
- NSString *classKey = NSStringFromClass(self);
- sharedInstances = sharedInstances ?: [[NSMutableDictionary alloc] init];
- id oldInstance = [sharedInstances objectForKey:classKey];
- if (instance)
+ if (property)
{
- [sharedInstances setObject:instance forKey:classKey];
+ [values setObject:property forKey:key];
}
else
{
- [sharedInstances removeObjectForKey:classKey];
+ [values removeObjectForKey:key];
+ }
+}
+
+
+#pragma mark -
+#pragma mark Singleton behaviour
+
++ (void)setSharedInstance:(BaseModel *)instance
+{
+ if (instance && ![instance isKindOfClass:self])
+ {
+ [NSException raise:NSGenericException format:@"setSharedInstance: instance class does not match"];
}
+ id oldInstance = [self classPropertyForKey:BaseModelSharedInstanceKey];
+ [self setClassProperty:instance forKey:BaseModelSharedInstanceKey];
if (oldInstance)
{
[[NSNotificationCenter defaultCenter] postNotificationName:BaseModelSharedInstanceUpdatedNotification object:oldInstance];
@@ -152,21 +178,19 @@ + (void)setSharedInstance:(BaseModel *)instance
+ (BOOL)hasSharedInstance
{
- return [sharedInstances objectForKey:NSStringFromClass(self)] != nil;
+ return [self classPropertyForKey:BaseModelSharedInstanceKey] != nil;
}
+ (instancetype)sharedInstance
{
- NSString *classKey = NSStringFromClass(self);
- sharedInstances = sharedInstances ?: [[NSMutableDictionary alloc] init];
- id instance = [sharedInstances objectForKey:classKey];
+ id instance = [self classPropertyForKey:BaseModelSharedInstanceKey];
if (instance == nil)
{
//load or create instance
[self reloadSharedInstance];
//get loaded instance
- instance = [sharedInstances objectForKey:classKey];
+ instance = [self classPropertyForKey:BaseModelSharedInstanceKey];
}
return instance;
}
@@ -206,7 +230,7 @@ - (BOOL)useHRCoderIfAvailable
- (void)save
{
- if ([sharedInstances objectForKey:NSStringFromClass([self class])] == self)
+ if ([[self class] classPropertyForKey:BaseModelSharedInstanceKey] == self)
{
//shared (singleton) instance
[self writeToFile:[[self class] saveFilePath] atomically:YES];
@@ -218,6 +242,7 @@ - (void)save
}
}
+
#pragma mark -
#pragma mark Default constructors
@@ -231,18 +256,16 @@ + (instancetype)instance
return [[[self alloc] init] autorelease];
}
-static BOOL loadingFromResourceFile = NO;
-
- (instancetype)init
{
- @synchronized ([BaseModel class])
+ @synchronized ([self class])
{
- if (!loadingFromResourceFile)
+ if (![[[self class] classPropertyForKey:BaseModelLoadingFromResourceFileKey] boolValue])
{
//attempt to load from resource file
- loadingFromResourceFile = YES;
+ [[self class] setClassProperty:[NSNumber numberWithBool:YES] forKey:BaseModelLoadingFromResourceFileKey];
id object = [[[self class] alloc] initWithContentsOfFile:[[self class] resourceFilePath]];
- loadingFromResourceFile = NO;
+ [[self class] setClassProperty:nil forKey:BaseModelLoadingFromResourceFileKey];
if (object)
{
[self release];
@@ -496,6 +519,7 @@ - (void)writeToFile:(NSString *)path atomically:(BOOL)atomically
[data writeToFile:[[self class] saveFilePath:path] atomically:YES];
}
+
#pragma mark -
#pragma mark Unique identifier generation
2  LICENCE.md
View
@@ -1,6 +1,6 @@
BaseModel
-version 2.3.4, October 25th, 2012
+version 2.3.5, October 29th, 2012
Copyright (C) 2011 Charcoal Design
8 README.md
View
@@ -19,9 +19,9 @@ BaseModel is also designed to work with the CryptoCoding library (https://github
Supported OS & SDK Versions
-----------------------------
-* Supported build target - iOS 6.0 / Mac OS 10.8 (Xcode 4.5, Apple LLVM compiler 4.1)
+* Supported build target - iOS 6.0 / Mac OS 10.8 (Xcode 4.5.1, Apple LLVM compiler 4.1)
* Earliest supported deployment target - iOS 5.0 / Mac OS 10.7
-* Earliest compatible deployment target - iOS 4.0 / Mac OS 10.6
+* Earliest compatible deployment target - iOS 4.3 / Mac OS 10.6
NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.
@@ -35,13 +35,13 @@ As of version 1.2.1, BaseModel automatically works with both ARC and non-ARC pro
Thread Safety
--------------
-You can create BaseModel instances on any thread, but each instance should only be used on a single thread. The sharedInstance methods should only be used on the main thread.
+BaseModel instances can only be safely created on the main thread. Once created, it should be safe to access them from multiple threads, provided that any custom methods and properties that are added by the user are thread-safe.
Installation
--------------
-To use the BaseModel class in your project, just drag the class files into your project. It has no dependencies, however you may wish to also include the AutoCoding library (https://github.com/nicklockwood/AutoCoding) to get the full benefit of using BaseModel.
+To use the BaseModel class in your project, just drag the BaseModel.h and .m files into your project. BaseModel has no required dependencies, however you may wish to also include the optional AutoCoding library (https://github.com/nicklockwood/AutoCoding) to get the full benefits of using BaseModel, and you may also wish to include the HRCoding (https://github.com/nicklockwood/HRCoding) and CryptoCoding (https://github.com/nicklockwood/CryptoCoding) libraries to enable additional BaseModel functionality.
Classes
4 RELEASE NOTES.md
View
@@ -1,3 +1,7 @@
+Version 2.3.5
+
+- Fixed a bug when creating BaseModel instances that are initialised with a resource file from within the init method of another BaseModel instance.
+
Version 2.3.4
- Fixed bug in resource file caching mechanism
Please sign in to comment.
Something went wrong with that request. Please try again.