我的印象是“超出允许时间的主动断言”错误是在首次加载应用时出现的。但是,今天我正在测试我的应用程序,使用iOS 7从Facebook进行批量导入,应用程序因此错误而崩溃。
具体来说,我打电话给:
[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
从朋友选择器(两个不同的网址)中选择的每个朋友两次。当我试图导入10个人时发生了崩溃。
我很困惑 - 到目前为止,该应用程序已完全启动。为什么这个断言发生在这里?
崩溃日志部分:
Incident Identifier: 0A705B17-FE23-4585-B3EF-89E34F4A4E89
CrashReporter Key: a9ef164d188c1490dffbe7fdd3c467823e2f3c19
Hardware Model: iPhone3,1
Process: MyApp [3366]
Path: /var/mobile/Applications/9CEDB552-C091-4FD7-AF1F-BE39A9D07C67/MyApp.app/MyApp
Identifier: com.mycom.myApp
Version: 1.5.2 (1.5.2)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2013-11-24 14:56:00.894 -0500
OS Version: iOS 7.0.3 (11B511)
Report Version: 104
Exception Type: 00000020
Exception Codes: 0x000000008badf00d
Highlighted Thread: 0
Application Specific Information:
MyApp[3366] has active assertions beyond permitted time:
{(
<BKProcessAssertion: 0x145564c0> identifier: Suspending process: MyApp[3366] permittedBackgroundDuration: 10.000000 reason: suspend owner pid:28 preventSuspend preventThrottleDownCPU preventThrottleDownUI preventSuspendOnSleep
)}
Elapsed total CPU time (seconds): 3.010 (user 3.010, system 0.000), 49% CPU
Elapsed application CPU time (seconds): 0.005, 0% CPU
Thread 0:
0 libsystem_kernel.dylib 0x3a6b9adc semaphore_wait_trap + 8
1 libdispatch.dylib 0x3a603fa0 _dispatch_semaphore_wait_slow + 172
2 CFNetwork 0x2f9426e0 CFURLConnectionSendSynchronousRequest + 264
3 Foundation 0x3070a7ae +[NSURLConnection sendSynchronousRequest:returningResponse:error:] + 110
4 Foundation 0x306da8c6 -[NSData(NSData) initWithContentsOfURL:] + 182
5 Foundation 0x306da7f6 +[NSData(NSData) dataWithContentsOfURL:] + 38
6 MyApp 0x0020e1a8 +[FacebookHelper imageForObject:] (FacebookHelper.m:910)
7 MyApp 0x0021bfe4 -[PersonViewController(Facebook) importFriend:withCompletion:] (PersonViewController+Facebook.m:110)
8 MyApp 0x0021bc2a -[PersonViewController(Facebook) importFriends:] (PersonViewController+Facebook.m:70)
9 Foundation 0x30758386 __NSFireDelayedPerform + 410
10 CoreFoundation 0x2fd490dc __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 12
11 CoreFoundation 0x2fd48cf2 __CFRunLoopDoTimer + 778
12 CoreFoundation 0x2fd4708e __CFRunLoopRun + 1206
13 CoreFoundation 0x2fcb1c22 CFRunLoopRunSpecific + 518
14 CoreFoundation 0x2fcb1a06 CFRunLoopRunInMode + 102
15 GraphicsServices 0x3499027e GSEventRunModal + 134
16 UIKit 0x32555044 UIApplicationMain + 1132
17 MyApp 0x000fc146 main (main.m:5)
18 libdyld.dylib 0x3a615ab4 start + 0
以下是代码段:
+ (UIImage *)imageForObject:(NSString *)objectID {
__ENTERING_METHOD__
NSString *url = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?width=%d&height=%d",objectID,IMAGE_MAXWIDTH,IMAGE_MAXHEIGHT];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
return image;
}
编辑:这是否允许我使用返回的图像将我的主线程保存到我的app委托managedObjectContext?换句话说,是线程安全的,还是我需要创建和管理一个全新的managedObjectContext然后合并它等等?与上述相反,对这个修改过的方法进行多次紧密间隔调用会帮助我避免“超出允许时间的主动断言”错误吗?
+ (UIImage *)imageForObject:(NSString *)objectID {
NSString *url = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?width=%d&height=%d",objectID,IMAGE_MAXWIDTH,IMAGE_MAXHEIGHT];
__block UIImage *image;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
dispatch_async(dispatch_get_main_queue(), ^{
image = [[UIImage alloc] initWithData:data];
});
});
return image;
}