2014-04-15 2 views
1

애플리케이션에서 다음 코드를 사용하여 didFinishLaunchingWithOptions 애플리케이션의 끝에 핵심 데이터에 액세스하려고합니다. 이것은 일반적으로 응용 프로그램 (I 다른 곳에서 동일한 코드를 여러 번 사용)에 걸쳐 아무런 문제가 없지만, 당신이 아래에 볼 수있는이 시점에서 충돌이 내가 didFinishLaunchingWithOptions (uploadPossibleDrives)에서 호출 코드가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; 
} 

:

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` 

정말 응용 프로그램은 '에너지'의 전무를 얻을 이유를 잘 모릅니다 : 여기

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]; 
       } 

      } 

      ... 

} 

은 충돌 보고서입니다. 다른 사람들은 didFinishLaunchingWithOptions와 다른 앱에서 왜 이런 일이 일어나는 지 알고 있습니까? 시간을내어 주셔서 감사합니다.

+2

디버그 용 로그 및 중단 점이있는 코드를'history.energy == nil'이있는 경우에 추가하고이 시점에서 어떻게되는지 이해하려고 시도하십시오 – sage444

+0

'에너지'선언 방법은 무엇입니까? – Amar

+0

NSNumber로. – freshking

답변

0

글쎄 내 애플 리케이션의 다른 지점에서 변경 가능한 배열은 때로는 에너지 및 energy_onlyDrive의 값을 포함하는 사전 릴리스되고 밝혀졌다. 그래서 그것들은 사실상 아무렇지 않게 설정되었습니다. 도움을 주신 모든 분들께 감사드립니다.