Skip to content

Commit 33f7c3f

Browse files
committed
调整细节
1.合理调整全局变量的创建时间 2.线程同步
1 parent 71f94be commit 33f7c3f

File tree

8 files changed

+129
-92
lines changed

8 files changed

+129
-92
lines changed

MJExtension.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "MJExtension"
3-
s.version = "3.0.13"
3+
s.version = "3.0.14"
44
s.ios.deployment_target = '6.0'
55
s.osx.deployment_target = '10.8'
66
s.summary = "A fast and convenient conversion between JSON and model"

MJExtension/MJExtensionConst.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
#import <Foundation/Foundation.h>
66

7+
// 信号量
8+
#define MJExtensionSemaphoreCreate \
9+
static dispatch_semaphore_t signalSemaphore; \
10+
static dispatch_once_t onceTokenSemaphore; \
11+
dispatch_once(&onceTokenSemaphore, ^{ \
12+
signalSemaphore = dispatch_semaphore_create(1); \
13+
});
14+
15+
#define MJExtensionSemaphoreWait \
16+
dispatch_semaphore_wait(signalSemaphore, DISPATCH_TIME_FOREVER);
17+
18+
#define MJExtensionSemaphoreSignal \
19+
dispatch_semaphore_signal(signalSemaphore);
20+
721
// 过期
822
#define MJExtensionDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)
923

@@ -85,4 +99,4 @@ extern NSString *const MJPropertyTypeClass;
8599
extern NSString *const MJPropertyTypeSEL;
86100
extern NSString *const MJPropertyTypeId;
87101

88-
#endif
102+
#endif

MJExtension/MJFoundation.m

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
#import "MJExtensionConst.h"
1111
#import <CoreData/CoreData.h>
1212

13-
static NSSet *foundationClasses_;
14-
1513
@implementation MJFoundation
1614

17-
+ (NSSet *)foundationClasses
15+
+ (BOOL)isClassFromFoundation:(Class)c
1816
{
19-
if (foundationClasses_ == nil) {
17+
if (c == [NSObject class] || c == [NSManagedObject class]) return YES;
18+
19+
static NSSet *foundationClasses;
20+
static dispatch_once_t onceToken;
21+
dispatch_once(&onceToken, ^{
2022
// 集合中没有NSObject,因为几乎所有的类都是继承自NSObject,具体是不是NSObject需要特殊判断
21-
foundationClasses_ = [NSSet setWithObjects:
23+
foundationClasses = [NSSet setWithObjects:
2224
[NSURL class],
2325
[NSDate class],
2426
[NSValue class],
@@ -28,16 +30,10 @@ + (NSSet *)foundationClasses
2830
[NSDictionary class],
2931
[NSString class],
3032
[NSAttributedString class], nil];
31-
}
32-
return foundationClasses_;
33-
}
34-
35-
+ (BOOL)isClassFromFoundation:(Class)c
36-
{
37-
if (c == [NSObject class] || c == [NSManagedObject class]) return YES;
33+
});
3834

3935
__block BOOL result = NO;
40-
[[self foundationClasses] enumerateObjectsUsingBlock:^(Class foundationClass, BOOL *stop) {
36+
[foundationClasses enumerateObjectsUsingBlock:^(Class foundationClass, BOOL *stop) {
4137
if ([c isSubclassOfClass:foundationClass]) {
4238
result = YES;
4339
*stop = YES;

MJExtension/MJProperty.m

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ - (instancetype)init
3131
#pragma mark - 缓存
3232
+ (instancetype)cachedPropertyWithProperty:(objc_property_t)property
3333
{
34+
MJExtensionSemaphoreCreate
35+
MJExtensionSemaphoreWait
3436
MJProperty *propertyObj = objc_getAssociatedObject(self, property);
3537
if (propertyObj == nil) {
3638
propertyObj = [[self alloc] init];
3739
propertyObj.property = property;
3840
objc_setAssociatedObject(self, property, propertyObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
3941
}
42+
MJExtensionSemaphoreSignal
4043
return propertyObj;
4144
}
4245

@@ -150,21 +153,39 @@ - (void)setOriginKey:(id)originKey forClass:(Class)c
150153
- (void)setPorpertyKeys:(NSArray *)propertyKeys forClass:(Class)c
151154
{
152155
if (propertyKeys.count == 0) return;
153-
self.propertyKeysDict[NSStringFromClass(c)] = propertyKeys;
156+
NSString *key = NSStringFromClass(c);
157+
if (!key) return;
158+
159+
MJExtensionSemaphoreCreate
160+
MJExtensionSemaphoreWait
161+
self.propertyKeysDict[key] = propertyKeys;
162+
MJExtensionSemaphoreSignal
154163
}
164+
155165
- (NSArray *)propertyKeysForClass:(Class)c
156166
{
157-
return self.propertyKeysDict[NSStringFromClass(c)];
167+
NSString *key = NSStringFromClass(c);
168+
if (!key) return nil;
169+
return self.propertyKeysDict[key];
158170
}
159171

160172
/** 模型数组中的模型类型 */
161173
- (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c
162174
{
163175
if (!objectClass) return;
164-
self.objectClassInArrayDict[NSStringFromClass(c)] = objectClass;
176+
NSString *key = NSStringFromClass(c);
177+
if (!key) return;
178+
179+
MJExtensionSemaphoreCreate
180+
MJExtensionSemaphoreWait
181+
self.objectClassInArrayDict[key] = objectClass;
182+
MJExtensionSemaphoreSignal
165183
}
184+
166185
- (Class)objectClassInArrayForClass:(Class)c
167186
{
168-
return self.objectClassInArrayDict[NSStringFromClass(c)];
187+
NSString *key = NSStringFromClass(c);
188+
if (!key) return nil;
189+
return self.objectClassInArrayDict[key];
169190
}
170191
@end

MJExtension/MJPropertyType.m

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313

1414
@implementation MJPropertyType
1515

16-
static NSMutableDictionary *types_;
17-
+ (void)initialize
18-
{
19-
types_ = [NSMutableDictionary dictionary];
20-
}
21-
2216
+ (instancetype)cachedTypeWithCode:(NSString *)code
2317
{
2418
MJExtensionAssertParamNotNil2(code, nil);
25-
@synchronized (self) {
26-
MJPropertyType *type = types_[code];
27-
if (type == nil) {
28-
type = [[self alloc] init];
29-
type.code = code;
30-
types_[code] = type;
31-
}
32-
return type;
19+
20+
static NSMutableDictionary *types;
21+
static dispatch_once_t onceToken;
22+
dispatch_once(&onceToken, ^{
23+
types = [NSMutableDictionary dictionary];
24+
});
25+
26+
MJExtensionSemaphoreCreate
27+
MJExtensionSemaphoreWait
28+
MJPropertyType *type = types[code];
29+
if (type == nil) {
30+
type = [[self alloc] init];
31+
type.code = code;
32+
types[code] = type;
3333
}
34+
MJExtensionSemaphoreSignal
35+
return type;
3436
}
3537

3638
#pragma mark - 公共方法

MJExtension/NSObject+MJClass.m

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@
1717
static const char MJAllowedCodingPropertyNamesKey = '\0';
1818
static const char MJIgnoredCodingPropertyNamesKey = '\0';
1919

20-
static NSMutableDictionary *allowedPropertyNamesDict_;
21-
static NSMutableDictionary *ignoredPropertyNamesDict_;
22-
static NSMutableDictionary *allowedCodingPropertyNamesDict_;
23-
static NSMutableDictionary *ignoredCodingPropertyNamesDict_;
24-
2520
@implementation NSObject (MJClass)
2621

27-
+ (void)load
28-
{
29-
allowedPropertyNamesDict_ = [NSMutableDictionary dictionary];
30-
ignoredPropertyNamesDict_ = [NSMutableDictionary dictionary];
31-
allowedCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
32-
ignoredCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
33-
}
34-
3522
+ (NSMutableDictionary *)dictForKey:(const void *)key
3623
{
37-
@synchronized (self) {
38-
if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_;
39-
if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_;
40-
if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_;
41-
if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_;
42-
return nil;
43-
}
24+
static NSMutableDictionary *allowedPropertyNamesDict;
25+
static NSMutableDictionary *ignoredPropertyNamesDict;
26+
static NSMutableDictionary *allowedCodingPropertyNamesDict;
27+
static NSMutableDictionary *ignoredCodingPropertyNamesDict;
28+
29+
static dispatch_once_t onceToken;
30+
dispatch_once(&onceToken, ^{
31+
allowedPropertyNamesDict = [NSMutableDictionary dictionary];
32+
ignoredPropertyNamesDict = [NSMutableDictionary dictionary];
33+
allowedCodingPropertyNamesDict = [NSMutableDictionary dictionary];
34+
ignoredCodingPropertyNamesDict = [NSMutableDictionary dictionary];
35+
});
36+
37+
if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict;
38+
if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict;
39+
if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict;
40+
if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict;
41+
return nil;
4442
}
4543

4644
+ (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration
@@ -130,6 +128,7 @@ + (NSMutableArray *)mj_totalAllowedCodingPropertyNames
130128
{
131129
return [self mj_totalObjectsWithSelector:@selector(mj_allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey];
132130
}
131+
133132
#pragma mark - block和方法处理:存储block的返回值
134133
+ (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key
135134
{
@@ -145,26 +144,32 @@ + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key
145144

146145
+ (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key
147146
{
148-
NSMutableArray *array = [self dictForKey:key][NSStringFromClass(self)];
149-
if (array) return array;
150-
151-
// 创建、存储
152-
[self dictForKey:key][NSStringFromClass(self)] = array = [NSMutableArray array];
147+
MJExtensionSemaphoreCreate
148+
MJExtensionSemaphoreWait
153149

154-
if ([self respondsToSelector:selector]) {
155-
#pragma clang diagnostic push
156-
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
157-
NSArray *subArray = [self performSelector:selector];
158-
#pragma clang diagnostic pop
159-
if (subArray) {
160-
[array addObjectsFromArray:subArray];
150+
NSMutableArray *array = [self dictForKey:key][NSStringFromClass(self)];
151+
if (array == nil) {
152+
// 创建、存储
153+
[self dictForKey:key][NSStringFromClass(self)] = array = [NSMutableArray array];
154+
155+
if ([self respondsToSelector:selector]) {
156+
#pragma clang diagnostic push
157+
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
158+
NSArray *subArray = [self performSelector:selector];
159+
#pragma clang diagnostic pop
160+
if (subArray) {
161+
[array addObjectsFromArray:subArray];
162+
}
161163
}
164+
165+
[self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
166+
NSArray *subArray = objc_getAssociatedObject(c, key);
167+
[array addObjectsFromArray:subArray];
168+
}];
162169
}
163170

164-
[self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
165-
NSArray *subArray = objc_getAssociatedObject(c, key);
166-
[array addObjectsFromArray:subArray];
167-
}];
171+
MJExtensionSemaphoreSignal
172+
168173
return array;
169174
}
170175
@end

MJExtension/NSObject+MJProperty.m

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,29 @@
2727

2828
@implementation NSObject (Property)
2929

30-
static NSMutableDictionary *replacedKeyFromPropertyNameDict_;
31-
static NSMutableDictionary *replacedKeyFromPropertyName121Dict_;
32-
static NSMutableDictionary *newValueFromOldValueDict_;
33-
static NSMutableDictionary *objectClassInArrayDict_;
34-
static NSMutableDictionary *cachedPropertiesDict_;
35-
36-
+ (void)load
37-
{
38-
replacedKeyFromPropertyNameDict_ = [NSMutableDictionary dictionary];
39-
replacedKeyFromPropertyName121Dict_ = [NSMutableDictionary dictionary];
40-
newValueFromOldValueDict_ = [NSMutableDictionary dictionary];
41-
objectClassInArrayDict_ = [NSMutableDictionary dictionary];
42-
cachedPropertiesDict_ = [NSMutableDictionary dictionary];
43-
}
44-
4530
+ (NSMutableDictionary *)dictForKey:(const void *)key
4631
{
47-
@synchronized (self) {
48-
if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_;
49-
if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_;
50-
if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_;
51-
if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_;
52-
if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_;
53-
return nil;
54-
}
32+
static NSMutableDictionary *replacedKeyFromPropertyNameDict;
33+
static NSMutableDictionary *replacedKeyFromPropertyName121Dict;
34+
static NSMutableDictionary *newValueFromOldValueDict;
35+
static NSMutableDictionary *objectClassInArrayDict;
36+
static NSMutableDictionary *cachedPropertiesDict;
37+
38+
static dispatch_once_t onceToken;
39+
dispatch_once(&onceToken, ^{
40+
replacedKeyFromPropertyNameDict = [NSMutableDictionary dictionary];
41+
replacedKeyFromPropertyName121Dict = [NSMutableDictionary dictionary];
42+
newValueFromOldValueDict = [NSMutableDictionary dictionary];
43+
objectClassInArrayDict = [NSMutableDictionary dictionary];
44+
cachedPropertiesDict = [NSMutableDictionary dictionary];
45+
});
46+
47+
if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict;
48+
if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict;
49+
if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict;
50+
if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict;
51+
if (key == &MJCachedPropertiesKey) return cachedPropertiesDict;
52+
return nil;
5553
}
5654

5755
#pragma mark - --私有方法--

MJExtensionExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@
881881
C37522BF1CF0AC330037E747 /* Release */,
882882
);
883883
defaultConfigurationIsVisible = 0;
884+
defaultConfigurationName = Release;
884885
};
885886
/* End XCConfigurationList section */
886887
};

0 commit comments

Comments
 (0)