1

我们dateFromComponents:用于解析从服务器返回的日期字符串。

在极少数情况下,此调用会崩溃,但我们无法重现该错误。有没有人见过这种堆栈跟踪?

Thread 0:
0   libicucore.A.dylib                  0x3b13514e _uprv_asciitolower + 18
1   libicucore.A.dylib                  0x3b18757b _uloc_openKeywordList + 291
2   libicucore.A.dylib                  0x3b135d7f _uloc_getName + 15
3   libicucore.A.dylib                  0x3b135ba7 __ZN3icu6Locale4initEPKca + 175
4   libicucore.A.dylib                  0x3b14f359 __ZN3icu6Locale14createFromNameEPKc + 57
5   libicucore.A.dylib                  0x3b1b13db __ZN3icu8Calendar11setWeekDataERKNS_6LocaleEPKcR10UErrorCode + 131
6   libicucore.A.dylib                  0x3b1b164f __ZN3icu8CalendarC2ERKNS_8TimeZoneERKNS_6LocaleER10UErrorCode + 123
7   libicucore.A.dylib                  0x3b1d5857 __ZN3icu17GregorianCalendarC2ERKNS_8TimeZoneER10UErrorCode + 27
8   libicucore.A.dylib                  0x3b1d5d7b __ZN3icu17GregorianCalendar18setGregorianChangeEdR10UErrorCode + 163
9   CoreFoundation                      0x33728967 __CFCalendarSetupCal + 95
10  CoreFoundation                      0x336a5db1 __CFCalendarComposeAbsoluteTimeV + 41
11  CoreFoundation                      0x336b70d3 -[__NSCFCalendar dateFromComponents:] + 1139
12  <OurApp>                            0x0015d7c5 -[NSString(<ACategory>) parsedDateFromyyyyMMddFormat] + 493
13  <OurApp>                            0x000e28ab -[<AnNSOperation> finishedWithData:serverDate:] (<AnNSOperation>.m:154)
14  <OurApp>                            0x0009ba87 __49-[<AnotherNSOperation> connectionDidFinishLoading:]_block_invoke (<AnotherNSOperation>.m:283)
15  libdispatch.dylib                   0x3b9d0793 _dispatch_call_block_and_release + 11
16  libdispatch.dylib                   0x3b9d05db _dispatch_client_callout + 23
17  libdispatch.dylib                   0x3b9d3e45 __dispatch_main_queue_callback_4CF + 229
18  CoreFoundation                      0x336f01b1 __CFRunLoopRun + 1289
19  CoreFoundation                      0x3366323d _CFRunLoopRunSpecific + 357
20  CoreFoundation                      0x336630c9 _CFRunLoopRunInMode + 105
21  GraphicsServices                    0x3721e33b _GSEventRunModal + 75
22  UIKit                               0x3557f2b9 _UIApplicationMain + 1121
23  <OurApp>                            0x0006c151 main (main.m:32)

我们还观察到这样的堆栈跟踪:

Thread 20 Crashed:
0   libsystem_kernel.dylib              0x3c4fb350 ___pthread_kill + 8
1   libsystem_c.dylib                   0x3c4ae36b _abort + 95
2   libc++abi.dylib                     0x3ba56ddf abort_message + 75
3   libc++abi.dylib                     0x3ba541cf ___cxa_pure_virtual + 19
4   libicucore.A.dylib                  0x3bc02637 __ZN3icu8CalendarC2ERKNS_8TimeZoneERKNS_6LocaleER10UErrorCode + 99
5   libicucore.A.dylib                  0x3bc26857 __ZN3icu17GregorianCalendarC2ERKNS_8TimeZoneER10UErrorCode + 27
6   libicucore.A.dylib                  0x3bc26d7b __ZN3icu17GregorianCalendar18setGregorianChangeEdR10UErrorCode + 163
7   CoreFoundation                      0x34189967 __CFCalendarSetupCal + 95
8   CoreFoundation                      0x34106db1 __CFCalendarComposeAbsoluteTimeV + 41
9   CoreFoundation                      0x341180d3 -[__NSCFCalendar dateFromComponents:] + 1139

以下是方法:

- (NSDate *)parsedDateFromyyyyMMddFormat
{
    NSDateComponents* dateComp = [[NSDateComponents alloc] init];

    NSArray *stringParts = [self componentsSeparatedByString:@"-"];

    /* 
       check stringParts validity 
       ...
       get years, month & co from "stringParts"
       ...
       set year, month & co on "dateComp"
    */

    >>>> THIS LINE CRASHED 
    NSDate *date = [[[self class] currentCalendar] dateFromComponents: dateComp]; 
    <<<<

    return date;
}


+ (NSCalendar *)currentCalendar
{
    static NSCalendar *calendar;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        calendar = [NSCalendar currentCalendar];
        [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"]];
    });

    return calendar;
}
4

1 回答 1

5

如果某事随机且很少发生,则它是并发问题的危险信号。

NSCalendar 不是线程安全的,请确保应用程序中当前日历的每次使用都在同一个线程上。

另外,我不知道 Arc 是否足够聪明,不会保留你已经单例的日历两次。

于 2013-08-29T13:09:16.293 回答