- 在我的數據模型中有車輛實體。
- 它具有名稱,品牌,型號等屬性。
- 我已經有一個模式繼承NewVehicleViewController,它允許用戶輸入實體信息。
- 這賽格瑞
單擊「完成」,創建通過調用來自NewVehicleViewController的IBAction爲方法中的創建+ vehicle.m法新NSEntityDescription期間我通過managedObjectContext到NewVehicleViewController。創建NSEntityDescription後拋出異常
Vehicle * car = [Vehicle vehicleWithName:name inManagedObjectContext:self.context];
此方法執行以下操作:
+ (Vehicle *) vehicleWithName:(NSString *)name inManagedObjectContext:(NSManagedObjectContext *) context
{
Vehicle *vehicle = nil;
//check for duplicates
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Vehicle"];
request.predicate = [NSPredicate predicateWithFormat:@"name = %@", name];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error;
NSArray *matches = [context executeFetchRequest:request error:&error];
NSLog(@"Matches: %d", matches.count);
if(!matches || [matches count] >1){
//nil or more than 1
} else if ([matches count] == 0){
//not found
vehicle = [NSEntityDescription insertNewObjectForEntityForName:@"Vehicle" inManagedObjectContext:context];
vehicle.name = name;
}else{
vehicle = [matches lastObject];
NSLog(@"vehicle already exists with name: %@", name);
}
NSLog(@"Created vehicle with name: %@", vehicle.name);
return vehicle;
}
窗口關閉後,我返回到我的表視圖控制器,我可以看到有新的元素。 (該錶鏈接到提取語句)。一切都很好。
然後大約4-6秒後,我得到一個異常拋出。底部的調試日誌沒有顯示任何內容,它將我帶入第8行視圖,第一行顯示中斷。
libobjc.A.dylib`objc_exception_throw:
0x1780caa: pushl %ebp
0x1780cab: movl %esp, %ebp
0x1780cad: pushl %ebx
0x1780cae: pushl %edi
0x1780caf: pushl %esi
0x1780cb0: subl $2028, %esp
0x1780cb6: calll 0x01780cbb ; objc_exception_throw + 17
所以我的第一個問題是,任何想法是什麼問題?這是由於NS核心數據的自動保存在一段時間後發生的嗎?
而我的第二個問題是,我怎麼能調試這進一步找出問題是什麼?
謝謝!
在這一點上我不知道你的第一個問題。對於第二種情況,有時可以在main.m中的行中包裝@ try/@ catch塊來啓動應用程序。或者,對於類似的結果,請調用'NSSetUncaughtExceptionHandler'。無論哪種情況,都要記錄異常描述及其'callStackSymbols'。 –
你說的是做這樣的事情: ' INT主(INT ARGC,CHAR *的argv []){ @try {// 試一下 @autoreleasepool { 回報UIApplicationMain(ARGC,ARGV,零, NSStringFromClass([DashAppDelegate class])); (NSException * e){ } NSLog(@「Exception:%@」,e); } @finally {// 添加到顯示最後的作品,以及 }} ' 我 – Jonathan
通常只是把它周圍的'回...'但我懷疑你的方法是OK了。確保你也記錄了'[callStackSymbols]'。 –