2016-02-28 79 views
2

我的iOS使用Bugsnag,我试图把它从4.1.0版本升级到版本5Bugsnag:缺少功能mergeWith升级到版本时5

新的SDK突破,在4版本可用的功能.X:

[[[Bugsnag configuration] metaData] mergeWith:parameters]; 

参数NSDictionary类型。

我找不到在SDK中的任何替代品,除了:

- (void)addAttribute:(NSString*)attributeName withValue:(id)value toTabWithName:(NSString*)tabName 

但它不提供相同的功能,其中value可能是一个NSDictionary本身。此外,它还会在每次添加时调用[self.delegate metaDataChanged:self](非常低效)。

回答

2

在查看Github repository并查看版本之间的差异BugsnagMetaData后,我找到了恢复此功能的方法。我写的扩展类类别:

@interface BugsnagMetaData (BugsnagExtension) 

- (void)mergeWith:(NSDictionary *)data; 

@end 

@implementation BugsnagMetaData (BugsnagExtension) 

- (void)mergeWith:(NSDictionary *)data { 
    @synchronized(self) { 
     NSString *customDataKey = @"customData"; 
     [data enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { 
      NSMutableDictionary *destination = [self getTab:customDataKey]; 
      if ([value isKindOfClass:[NSDictionary class]]) { 
       NSDictionary *source = value; 
       [source enumerateKeysAndObjectsUsingBlock:^(id sourceKey, id sourceValue, BOOL *stop) { 
        if ([destination objectForKey:sourceKey] && [sourceValue isKindOfClass:[NSDictionary class]]) { 
         [[destination objectForKey:sourceKey] mergeWith:(NSDictionary *)sourceValue]; 
        } else { 
         [destination setObject:sourceValue forKey:sourceKey]; 
        } 
       }]; 

      } else { 
       [destination setObject:value forKey:key]; 
      } 
     }]; 

     [self.delegate metaDataChanged:self]; 
    } 
} 

@end 

此功能是能够接受的NSDictionary包含的NSDictionary像以前一样,并且只在需要时调用效率[self.delegate metaDataChanged:self]