From 5e9f2f5a235e3bf5794f8227833167a5cd91b24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=E4=BA=86=E4=B8=AAJ?= Date: Thu, 21 Jul 2016 19:59:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决多线程环境下的Crash问题 --- MJExtension.podspec | 2 +- MJExtension/MJPropertyType.m | 15 ++++++++------- MJExtension/NSObject+MJClass.m | 12 +++++++----- MJExtension/NSObject+MJProperty.m | 14 ++++++++------ 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/MJExtension.podspec b/MJExtension.podspec index f312ea72..d7b49627 100644 --- a/MJExtension.podspec +++ b/MJExtension.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "MJExtension" - s.version = "3.0.11" + s.version = "3.0.12" s.ios.deployment_target = '6.0' s.osx.deployment_target = '10.8' s.summary = "A fast and convenient conversion between JSON and model" diff --git a/MJExtension/MJPropertyType.m b/MJExtension/MJPropertyType.m index b78b2028..384beba5 100755 --- a/MJExtension/MJPropertyType.m +++ b/MJExtension/MJPropertyType.m @@ -22,14 +22,15 @@ + (void)initialize + (instancetype)cachedTypeWithCode:(NSString *)code { MJExtensionAssertParamNotNil2(code, nil); - - MJPropertyType *type = types_[code]; - if (type == nil) { - type = [[self alloc] init]; - type.code = code; - types_[code] = type; + @synchronized (self) { + MJPropertyType *type = types_[code]; + if (type == nil) { + type = [[self alloc] init]; + type.code = code; + types_[code] = type; + } + return type; } - return type; } #pragma mark - 公共方法 diff --git a/MJExtension/NSObject+MJClass.m b/MJExtension/NSObject+MJClass.m index 789b9a89..46447a86 100644 --- a/MJExtension/NSObject+MJClass.m +++ b/MJExtension/NSObject+MJClass.m @@ -34,11 +34,13 @@ + (void)load + (NSMutableDictionary *)dictForKey:(const void *)key { - if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_; - if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_; - if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_; - if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_; - return nil; + @synchronized (self) { + if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_; + if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_; + if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_; + if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_; + return nil; + } } + (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration diff --git a/MJExtension/NSObject+MJProperty.m b/MJExtension/NSObject+MJProperty.m index fd7197a0..2314c460 100644 --- a/MJExtension/NSObject+MJProperty.m +++ b/MJExtension/NSObject+MJProperty.m @@ -44,12 +44,14 @@ + (void)load + (NSMutableDictionary *)dictForKey:(const void *)key { - if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_; - if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_; - if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_; - if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_; - if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_; - return nil; + @synchronized (self) { + if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_; + if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_; + if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_; + if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_; + if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_; + return nil; + } } #pragma mark - --私有方法--