1
在我的應用程序訪問核心數據,當我嘗試就在使用下面的代碼的應用程序didFinishLaunchingWithOptions結束訪問核心數據。這通常是整個應用程序(我在其他地方使用相同的代碼多次)沒有問題,但在這一點上崩潰,你可以看到如下:奇怪的崩潰從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'從這一點試着去了解這是如何發生 – sage444
如何屬性'energy'聲明? – Amar
作爲一個NSNumber。 – freshking