Skip to content

Commit

Permalink
fix potential problem when class updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ibireme committed Feb 29, 2016
1 parent e10f967 commit 95ca273
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion YYModel/NSObject+YYModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:)]);
Expand All @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions YYModel/YYClassInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions YYModel/YYClassInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ - (void)setNeedUpdate {
_needUpdate = YES;
}

- (BOOL)needUpdate {
return _needUpdate;
}

+ (instancetype)classInfoWithClass:(Class)cls {
if (!cls) return nil;
static CFMutableDictionaryRef classCache;
Expand Down

0 comments on commit 95ca273

Please sign in to comment.