Ios 从didFinishLaunchingWithOptions访问核心数据时发生奇怪的崩溃

Ios 从didFinishLaunchingWithOptions访问核心数据时发生奇怪的崩溃,ios,core-data,null,appdelegate,Ios,Core Data,Null,Appdelegate,在我的应用程序中,我尝试在应用程序didFinishLaunchingWithOptions结束时使用下面的代码访问核心数据。在整个应用程序中,这通常没有问题。我在其他地方多次使用相同的代码,但在这一点上,它崩溃了,如下所示: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIView *splashScreen

在我的应用程序中,我尝试在应用程序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,因为您获取了一个没有能量值的对象。您需要弄清楚创建该对象的方式和原因,并且在尝试插入值之前需要检查nil。