feat:Do not use mutable copy or copy to avoid change data pointer

This commit is contained in:
pengfei.zhou 2019-11-19 19:48:20 +08:00
parent 27db7f45fb
commit de6a094afa
3 changed files with 27 additions and 30 deletions

View File

@ -153,6 +153,7 @@ - (void)blendSubNode:(NSDictionary *)subModel {
NSMutableDictionary *model = [[self subModelOf:viewId] mutableCopy]; NSMutableDictionary *model = [[self subModelOf:viewId] mutableCopy];
[self recursiveMixin:subModel to:model]; [self recursiveMixin:subModel to:model];
[self setSubModel:model in:viewId]; [self setSubModel:model in:viewId];
}
[self.itemViewIds enumerateKeysAndObjectsUsingBlock:^(NSNumber *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) { [self.itemViewIds enumerateKeysAndObjectsUsingBlock:^(NSNumber *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) {
if ([viewId isEqualToString:obj]) { if ([viewId isEqualToString:obj]) {
*stop = YES; *stop = YES;
@ -163,7 +164,6 @@ - (void)blendSubNode:(NSDictionary *)subModel {
} }
}]; }];
} }
}
- (void)callItem:(NSUInteger)position height:(CGFloat)height { - (void)callItem:(NSUInteger)position height:(CGFloat)height {
NSNumber *old = self.itemHeights[@(position)]; NSNumber *old = self.itemHeights[@(position)];

View File

@ -171,6 +171,7 @@ - (void)blendSubNode:(NSDictionary *)subModel {
NSMutableDictionary *model = [[self subModelOf:viewId] mutableCopy]; NSMutableDictionary *model = [[self subModelOf:viewId] mutableCopy];
[self recursiveMixin:subModel to:model]; [self recursiveMixin:subModel to:model];
[self setSubModel:model in:viewId]; [self setSubModel:model in:viewId];
}
[self.itemViewIds enumerateKeysAndObjectsUsingBlock:^(NSNumber *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) { [self.itemViewIds enumerateKeysAndObjectsUsingBlock:^(NSNumber *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) {
if ([viewId isEqualToString:obj]) { if ([viewId isEqualToString:obj]) {
*stop = YES; *stop = YES;
@ -181,7 +182,6 @@ - (void)blendSubNode:(NSDictionary *)subModel {
} }
}]; }];
} }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSUInteger pageIndex = (NSUInteger) (scrollView.contentOffset.x / scrollView.width); NSUInteger pageIndex = (NSUInteger) (scrollView.contentOffset.x / scrollView.width);

View File

@ -35,7 +35,7 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop { - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
if ([@"subviews" isEqualToString:name]) { if ([@"subviews" isEqualToString:name]) {
NSArray *subviews = prop; NSArray *subviews = prop;
for (NSDictionary *subModel in subviews) { for (NSMutableDictionary *subModel in subviews) {
[self mixinSubNode:subModel]; [self mixinSubNode:subModel];
[self blendSubNode:subModel]; [self blendSubNode:subModel];
} }
@ -44,55 +44,52 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
} }
} }
- (void)mixinSubNode:(NSDictionary *)dictionary { - (void)mixinSubNode:(NSMutableDictionary *)dictionary {
NSString *viewId = dictionary[@"id"]; NSString *viewId = dictionary[@"id"];
NSMutableDictionary *oldModel = self.subNodes[viewId]; NSMutableDictionary *oldModel = self.subNodes[viewId];
if (oldModel) { if (oldModel) {
[self mixin:dictionary to:oldModel]; [self mixin:dictionary to:oldModel];
} else { } else {
self.subNodes[viewId] = [dictionary mutableCopy]; self.subNodes[viewId] = dictionary;
} }
} }
- (void)mixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel { - (void)mixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel {
NSDictionary *srcProp = srcModel[@"props"]; NSDictionary *srcProp = srcModel[@"props"];
NSMutableDictionary *targetProp = [targetModel[@"props"] mutableCopy]; NSMutableDictionary *targetProp = targetModel[@"props"];
[srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { [srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if (![@"subviews" isEqualToString:key]) { if (![@"subviews" isEqualToString:key]) {
targetProp[key] = obj; targetProp[key] = obj;
} }
}]; }];
targetModel[@"props"] = [targetProp copy]; targetModel[@"props"] = targetProp;
} }
- (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel { - (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel {
NSDictionary *srcProp = srcModel[@"props"]; NSDictionary *srcProp = srcModel[@"props"];
NSMutableDictionary *targetProp = [targetModel[@"props"] mutableCopy]; NSMutableDictionary *targetProp = targetModel[@"props"];
NSArray *targetOri = targetProp[@"subviews"]; NSMutableArray *targetOri = targetProp[@"subviews"];
[srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { [srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if ([@"subviews" isEqualToString:key]) { if ([@"subviews" isEqualToString:key]) {
NSArray *subviews = obj; NSArray *subviews = obj;
NSMutableArray *targetSubviews = [targetOri mutableCopy];
if (subviews) { if (subviews) {
for (NSDictionary *subview in subviews) { for (NSDictionary *subview in subviews) {
NSString *viewId = subview[@"id"]; NSString *viewId = subview[@"id"];
[targetSubviews enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) { [targetOri enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
if ([viewId isEqualToString:obj[@"id"]]) { if ([viewId isEqualToString:obj[@"id"]]) {
NSMutableDictionary *mutableDictionary = [obj mutableCopy]; NSMutableDictionary *mutableDictionary = [obj mutableCopy];
[self recursiveMixin:subview to:mutableDictionary]; [self recursiveMixin:subview to:mutableDictionary];
targetSubviews[idx] = [mutableDictionary copy]; targetOri[idx] = [mutableDictionary copy];
*stop = YES; *stop = YES;
} }
}]; }];
} }
targetProp[@"subviews"] = [targetSubviews copy];
} }
} else { } else {
targetProp[key] = obj; targetProp[key] = obj;
} }
}]; }];
targetModel[@"props"] = [targetProp copy];
} }
- (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig { - (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig {