firebase EXC_BAD_ACCESS logEventWithName

firebase EXC_BAD_ACCESS logEventWithName

使用 FIRAnalytics logEventWithName:parameters 方法时 EXC_BAD_ACCESS 导致应用程序崩溃。我是这样称呼它的:

[FIRAnalytics logEventWithName:name parameters:paramethers];

参数:

name = "News_Detail"

paramethers = {
    Title = "Lorem ipsum dolor is amet";
    Category = "Lorem Ipsum Category";
    URL = "https://api.loremipsum.com.uk/Contents/GalleryAlbum?appRef=iOSLoremIpsumApp&URL=%2floremipsum%2fdolor%2fisamet-loremipsum-loremipsum";
}

仅在此对象上崩溃。

我正在使用 cocoapods 在我的项目中添加库:

pod 'Google/Analytics' 

今天的当前版本是 3.9.0。

这是因为参数值最长可达100个字符。我对象的 URL 参数长 132 个字符。

下面是FIRAnalytics.h下的方法总结:

/// Logs an app event. The event can have up to 25 parameters. Events with the same name must have
/// the same parameters. Up to 500 event names are supported. Using predefined events and/or
/// parameters is recommended for optimal reporting.
///
/// The following event names are reserved and cannot be used:
/// <ul>
///     <li>app_clear_data</li>
///     <li>app_remove</li>
///     <li>app_update</li>
///     <li>error</li>
///     <li>first_open</li>
///     <li>in_app_purchase</li>
///     <li>notification_dismiss</li>
///     <li>notification_foreground</li>
///     <li>notification_open</li>
///     <li>notification_receive</li>
///     <li>os_update</li>
///     <li>session_start</li>
///     <li>user_engagement</li>
/// </ul>
///
/// @param name The name of the event. Should contain 1 to 40 alphanumeric characters or
///     underscores. The name must start with an alphabetic character. Some event names are
///     reserved. See FIREventNames.h for the list of reserved event names. The "firebase_" prefix
///     is reserved and should not be used. Note that event names are case-sensitive and that
///     logging two events whose names differ only in case will result in two distinct events.
/// @param parameters The dictionary of event parameters. Passing nil indicates that the event has
///     no parameters. Parameter names can be up to 40 characters long and must start with an
///     alphabetic character and contain only alphanumeric characters and underscores. Only NSString
///     and NSNumber (signed 64-bit integer and 64-bit floating-point number) parameter types are
///     supported. NSString parameter values can be up to 100 characters long. The "firebase_"
///     prefix is reserved and should not be used for parameter names.

警告

下面的这个 (trimParameters) 方法可以保证您 可能丢失数据 。请谨慎使用!它正在调整我们的参数。但是如果你不需要 100 个字符后的信息 "like in my case" 你可以使用它:

- (NSDictionary*)trimParameters:(NSDictionary*)paramethers {
    int maxCharacter = 100;
    NSMutableDictionary *result = [paramethers mutableCopy];

    for (NSString* key in result) {
        NSString *value  = [NSString stringWithFormat:@"%@", result[key]];
        if(![value isEqualToString:@""] && [value length] > maxCharacter) {
            result[key] = [value substringFromIndex:maxCharacter];
        }
    }

    return result;
}

我们可以这样调用这个方法:

[FIRAnalytics logEventWithName:name parameters:paramethers != nil ? [self trimParameters:paramethers] : paramethers];

解决方案:

正如@adbitx 在评论中所说:"crash has been fixed in the latest version 4.0.0." 对于安装版本“4.0.0” pod update 对我的情况没有帮助。

如果今天有人想使用版本“4.0.0”需要使用this pod:

pod 'FirebaseAnalytics'

或者等待其他pod库的更新。

希望对大家有所帮助。