diff --git a/YYModel/NSObject+YYModel.m b/YYModel/NSObject+YYModel.m index a50324c..c344265 100644 --- a/YYModel/NSObject+YYModel.m +++ b/YYModel/NSObject+YYModel.m @@ -404,6 +404,7 @@ + (instancetype)metaWithClassInfo:(YYClassInfo *)classInfo propertyInfo:(YYClass /// A class info in object model. @interface _YYModelMeta : NSObject { @package + YYClassInfo *_classInfo; /// Key:mapped key and key path, Value:_YYModelPropertyInfo. NSDictionary *_mapper; /// Array<_YYModelPropertyMeta>, all property meta of this model. @@ -554,6 +555,7 @@ - (instancetype)initWithClass:(Class)cls { if (keyPathPropertyMetas) _keyPathPropertyMetas = keyPathPropertyMetas; if (multiKeysPropertyMetas) _multiKeysPropertyMetas = multiKeysPropertyMetas; + _classInfo = classInfo; _keyMappedCount = _allPropertyMetas.count; _nsType = YYClassGetNSType(cls); _hasCustomTransformFromDictionary = ([cls instancesRespondToSelector:@selector(modelCustomTransformFromDictionary:)]); @@ -576,7 +578,7 @@ + (instancetype)metaWithClass:(Class)cls { dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); _YYModelMeta *meta = CFDictionaryGetValue(cache, (__bridge const void *)(cls)); dispatch_semaphore_signal(lock); - if (!meta) { + if (!meta || meta->_classInfo.needUpdate) { meta = [[_YYModelMeta alloc] initWithClass:cls]; if (meta) { dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); diff --git a/YYModel/YYClassInfo.h b/YYModel/YYClassInfo.h index 37c9085..a74e96f 100644 --- a/YYModel/YYClassInfo.h +++ b/YYModel/YYClassInfo.h @@ -159,11 +159,19 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding); If the class is changed (for example: you add a method to this class with 'class_addMethod()'), you should call this method to refresh the class info cache. - After called this method, you may call 'classInfoWithClass' or - 'classInfoWithClassName' to get the updated class info. + After called this method, `needUpdate` will returns `YES`, and you should call + 'classInfoWithClass' or 'classInfoWithClassName' to get the updated class info. */ - (void)setNeedUpdate; +/** + If this method returns `YES`, you should stop using this instance and call + `classInfoWithClass` or `classInfoWithClassName` to get the updated class info. + + @return Whether this class info need update. + */ +- (BOOL)needUpdate; + /** Get the class info of a specified Class. diff --git a/YYModel/YYClassInfo.m b/YYModel/YYClassInfo.m index 4e4ba55..15652b6 100644 --- a/YYModel/YYClassInfo.m +++ b/YYModel/YYClassInfo.m @@ -308,6 +308,10 @@ - (void)setNeedUpdate { _needUpdate = YES; } +- (BOOL)needUpdate { + return _needUpdate; +} + + (instancetype)classInfoWithClass:(Class)cls { if (!cls) return nil; static CFMutableDictionaryRef classCache;