Bugsnag: Missing function mergeWith when upgrading to version 5

62 Views Asked by At

My iOS is using Bugsnag and I am trying to upgrade it from version 4.1.0 to version 5.

The new SDK breaks a function that was available in version 4.x:

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

Where parameters is of type NSDictionary.

I couldn't find any substitute in the SDK, besides:

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

But it does not provide the same functionality where value could be a NSDictionary itself. Moreover, it will also invoke [self.delegate metaDataChanged:self] on every addition (highly inefficient).

1

There are 1 best solutions below

0
On BEST ANSWER

After looking in the Github repository and seeing the difference in BugsnagMetaData between the versions, I found a way to restore this functionality. I wrote a category that extends the class:

@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

This function is able to accept NSDictionary that contains NSDictionary like before, and calls [self.delegate metaDataChanged:self] efficiently only when needed.