2016-02-28 22 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]