[英]iPhone app crashes upon startup, don't understand why
My app uses an sqlite database managed with core-data. 我的应用程序使用通过核心数据管理的sqlite数据库。 These are the errors I found in the debug files.
这些是我在调试文件中发现的错误。 On the simulator it works just fine, no problems at all.
在模拟器上,它工作正常,完全没有问题。 Made the app in Xcode 4.2 iOS 5 and tried it on an iPhone 4 4.2.1.
在Xcode 4.2 iOS 5中制作了该应用程序,并在iPhone 4 4.2.1上进行了尝试。
Incident Identifier: 65E195D6-C583-4CA2-8017-1AEC56FAFBD4
CrashReporter Key: 4ebf8de224465a40c94ea40cffb742f345888ef0
Hardware Model: iPhone3,1
Process: Jobs [158]
Path: /Applications/Jobs.app/Jobs
Identifier: Jobs
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: punchd [1]
Date/Time: 2011-12-01 13:53:22.288 +0000
OS Version: iPhone OS 4.2.1 (8C148)
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0
Thread 0 Crashed:
4 Jobs 0x00002c74 0x1000 + 7284
5 Jobs 0x00002a0a 0x1000 + 6666
6 Jobs 0x00003012 0x1000 + 8210
36 Jobs 0x0000283c 0x1000 + 6204
37 Jobs 0x000027f4 0x1000 + 6132
Error 4 leads to this line of code: 错误4导致以下代码行:
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{ NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort(); //this one to be exact
}
Error 5 leads to this line of code: 错误5导致以下代码行:
- (NSManagedObjectContext *)managedObjectContext
{ if (__managedObjectContext != nil)
{ return __managedObjectContext; }
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; //this line with error;
if (coordinator != nil)
{ __managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator]; }
return __managedObjectContext;}
These were in the app delegate. 这些在应用程序委托中。 Now the 6th error is from the main view, in the ViewDidLoad:
现在第6个错误是来自主视图的ViewDidLoad:
if (managedObjectContext == nil)
{
managedObjectContext = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; //this line;
NSLog(@" %@", managedObjectContext);}
In response to request in comments: 针对评论中的要求:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyDatabase.sqlite"];
NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
...
Also: 也:
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Your app is not crashing, it is aborting as you instruct it to in abort
because this line of code is returning YES—as in, it didn't successfully addPersistentStoreWithType, since you are testing if(!
: 您的应用程序没有崩溃,它正在按您指示其中
abort
操作abort
因为此行代码返回YES,例如,由于您正在测试if(!
所以它没有成功addPersistentStoreWithType if(!
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
A good starting point to being able to solve this would be to show us the code that allocates/defines storeURL
, options
, and error
. 能够解决此问题的一个很好的起点是向我们展示分配/定义
storeURL
, options
和error
。 Most likely, one of these does not exist or has not been initialized correctly. 最有可能的原因之一是不存在或未正确初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.