从 didFinishLaunchingWithOptions 访问核心数据时出现奇怪的崩溃
Posted
技术标签:
【中文标题】从 didFinishLaunchingWithOptions 访问核心数据时出现奇怪的崩溃【英文标题】:Strange crash when accessing Core Data from didFinishLaunchingWithOptions 【发布时间】:2014-04-15 08:08:52 【问题描述】:在我的应用程序中,我尝试使用下面的代码在应用程序 didFinishLaunchingWithOptions 结束时访问核心数据。这通常在整个应用程序中都没有问题(我在其他地方多次使用相同的代码)但此时它会崩溃,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIView *splashScreen = [self splashScreen];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:splashScreen];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window setRootViewController:[[UIViewController alloc]init]];
[self.window makeKeyAndVisible];
[self configureRestKit];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^
... do stuff ...
dispatch_async(dispatch_get_main_queue(), ^
... more stuff (remove splash screen etc.) ...
HERE I CALL THE CODE! ---> [self uploadPossibleDrives];
);
);
return YES;
这是我从 didFinishLaunchingWithOptions (uploadPossibleDrives) 调用的代码:
NSManagedObjectContext *managedObjectContext = [kDelegate managedObjectContext];
if (!managedObjectContext) return;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"History" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"driveID" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil)
[kDelegate showAlert:kErrorTitle withMessage:kNoContextMessage andButton:kDismissButton];
else
while (history = [enumerator nextObject])
if (![controller containsObject:history.car])
NSMutableDictionary *driveDataByCar = [[NSMutableDictionary alloc] init];
[driveDataByCar setObject:history.car forKey:@"car"];
CRASH! -----> [driveDataByCar setObject:history.energy forKey:@"energy"];
[driveDataByCar setObject:history.energy_onlyDrive forKey:@"energy_only_drive"];
[driveDataByCar setObject:history.cw forKey:@"cw"];
[driveDataByCar setObject:history.weight forKey:@"weight"];
[driveDataByCar setObject:history.area forKey:@"area"];
[driveDataByCar setObject:history.maxPower forKey:@"maxPower"];
[carDataToUpload addObject:driveDataByCar];
[controller addObject:history.car];
...
这是崩溃报告:
Fatal Exception: NSInvalidArgumentException
*** setObjectForKey: object cannot be nil (key: energy)
0 CoreFoundation __exceptionPreprocess + 130
2 CoreFoundation -[__NSDictionaryM setObject:forKey:] + 818
3 App UploadDriveData.m line 140 -[UploadDriveData uploadDrivingData]
4 App AppDelegate.m line 302 __32-[AppDelegate uploadDrivingData]_block_invoke250
5 libdispatch.dylib _dispatch_call_block_and_release + 10
11 libsystem_pthread.dylib start_wqthread + 8
Crashed: Thread
SIGABRT ABORT at 0x3ba541f0
0 libsystem_kernel.dylib __pthread_kill + 8
9 CoreFoundation -[__NSDictionaryM setObject:forKey:] + 818
10 App UploadDriveData.m line 140 -[UploadDriveData uploadDrivingData]
11 App AppDelegate.m line 302 __32-[AppDelegate uploadDrivingData]_block_invoke250
12 libdispatch.dylib _dispatch_call_block_and_release + 10
17 libsystem_pthread.dylib _pthread_wqthread + 298`
我真的不知道为什么应用程序的“能量”为零。 其他任何人都知道为什么会在 didFinishLaunchingWithOptions 中发生这种情况,而在应用程序中的其他任何地方都没有? 感谢您抽出宝贵时间!
【问题讨论】:
用于调试,将带有日志和断点的代码添加到history.energy == nil
的情况下,然后尝试了解这是如何发生的
属性energy
是如何声明的?
你能NSLog
history
吗?
您正在尝试插入一个 nil,因为您获取的对象对于 energy
没有任何值。您需要弄清楚创建该对象的方式和原因,并且在尝试插入值之前需要检查 nil。
【参考方案1】:
事实证明,在我的应用程序的另一点上,有时会预先发布一个可变数组,其中包含 energy 和 energy_onlyDrive 的值。所以这些实际上设置为零。 谢谢大家的帮助!
【讨论】:
以上是关于从 didFinishLaunchingWithOptions 访问核心数据时出现奇怪的崩溃的主要内容,如果未能解决你的问题,请参考以下文章