在我的应用程序中,我尝试使用下面的代码在应用程序didFinishLaunchingWithOptions结束时访问Core Data。这在整个应用程序中通常没有问题(我在其他地方多次使用相同的代码),但此时它会崩溃,如下所示:
- (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中发生这种情况而在应用程序中没有其他地方? 谢谢您抽出宝贵时间!
答案 0 :(得分:0)
事实证明,在我的应用程序的另一个点上,一个可变数组有时会被预先释放,其中包含energy和energy_onlyDrive的值。所以那些实际上是零。 谢谢大家的帮助!