10

Crashlytics 在我的一个应用程序中报告了此崩溃,无论我做什么,我都无法重现它。这发生在大约 5% 的用户身上,所以这是一件大事。我正在发布带有崩溃报告的屏幕截图以及崩溃报告中提到的方法。知道如何解决这个问题吗?

崩溃报告

这是应用程序崩溃的地方:

#pragma mark - custom transformations
-(BOOL)__customSetValue:(id<NSObject>)value forProperty:(JSONModelClassProperty*)property
{
    if (!property.customSetters)
        property.customSetters = [NSMutableDictionary new];

    NSString *className = NSStringFromClass([JSONValueTransformer classByResolvingClusterClasses:[value class]]);

    if (!property.customSetters[className]) {
        //check for a custom property setter method
        NSString* ucfirstName = [property.name stringByReplacingCharactersInRange:NSMakeRange(0,1)
                                                                       withString:[[property.name substringToIndex:1] uppercaseString]];
        NSString* selectorName = [NSString stringWithFormat:@"set%@With%@:", ucfirstName, className];

        SEL customPropertySetter = NSSelectorFromString(selectorName);

        //check if there's a custom selector like this
        if (![self respondsToSelector: customPropertySetter]) {
            property.customSetters[className] = [NSNull null]; // this is line 855
            return NO;
        }

        //cache the custom setter selector
        property.customSetters[className] = selectorName;
    }

    if (property.customSetters[className] != [NSNull null]) {
        //call the custom setter
        //https://github.com/steipete
        SEL selector = NSSelectorFromString(property.customSetters[className]);
        ((void (*) (id, SEL, id))objc_msgSend)(self, selector, value);
        return YES;
    }

    return NO;
}

这是原始方法:

-(void)reloadUserInfoWithCompletion:(void (^) (LoginObject *response))handler andFailure:(void (^)(NSError *err))failureHandler {
    NSString *lat;
    NSString *lon;

    lat = [NSString stringWithFormat:@"%.6f",[[LocationManager sharedInstance] getPosition].coordinate.latitude];
    lon = [NSString stringWithFormat:@"%.6f",[[LocationManager sharedInstance] getPosition].coordinate.longitude];

    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setObject:lat forKey:@"latitude"];
    [params setObject:lon forKey:@"longitude"];

    [[LoginHandler sharedInstance] getLoginToken:^(NSString *response) {

        NSDictionary *headers;
        if (response) {
            headers = @{@"Login-Token":response};
        }
        GETRequest *req = [GETRequest new];
        [req setCompletionHandler:^(NSString *response) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSLog(@"response: %@",response);
                NSError *err = nil;
                self.loginObject.userDetails = [[User alloc] initWithString:response error:&err]; // <- this is the line reported in the crash
                [self storeLoginObject];
                NSLog(@"%@",self.loginObject.userDetails);
//                [Utils updateFiltersFullAccessIfAll]; 
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (handler) {
                        handler(self.loginObject);
                    }
                });
            });
        }];
        [req setFailedHandler:^(NSError *err) {
            if (failureHandler) {
                failureHandler(err);
            }
        }];
        NSLog(@"%@",params);
        [req requestWithLinkString:USER_DETAILS parameters:nil andHeaders:headers];
    }];

}
4

2 回答 2

2

所以setObject:forKey:可以通过两种方式引起问题。1. 如果objectnil或 2.keynil。两者都可能导致您看到的崩溃。鉴于您将其设置为object[NSNull null]假设它key给您带来问题可能是安全的(在第 855 行)。

从那里走回来会发现那classNamenil。如果你看,你的代码并不能防止这种情况发生。您在这里做出一个假设NSStringFromClass(之前的几行)正在给您返回一个有效的字符串,它假设value最初传递给方法的字符串是非nil. 如果是nil,它将通过您的所有检查,包括!property.customSetters[className],因为这将!nil允许它进入if.

如果我正确阅读了您的代码(有点困难,因为我无法测试我的任何假设)NSLog(@"response: %@",response);会打印出nil响应。

尝试查看您的代码如何处理这些意外nil的 s,并在评论中让我知道事情进展如何。

于 2016-01-30T01:03:29.303 回答
0

如果您不使用模型自定义设置器,您可以将 JSONModel __customSetValue:forProperty: 替换为 swizzling 或 Aspects 库

#import "JSONModel+Aspects.h"
#import "JSONModel.h"
#import "Aspects.h"

@implementation JSONModel (Aspects)

+(void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [JSONModel aspect_hookSelector:@selector(__customSetValue:forProperty:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> aspectInfo) {
            return NO;
        } error:NULL];
    });
}

@end
于 2016-04-20T12:20:48.507 回答