读书人

Core Data结构修改后在AppStore中更

发布时间: 2012-07-08 17:43:42 作者: rapoo

Core Data结构修改后,在AppStore中更新升级crash的问题.

问题描述:

在苹果的错误收集中有这么一个问题:

使用的xmpp框架,在调整为适应ios5的版本后出现一个导致程序crash问题。但是原来的xmpp代码没有改变,那么问题在哪呢?

报错如下:

view plain
  1. BUG监听报告:
  2. 手机型号: iPhone OS , 版本: 4.3
  3. 程序名称: xxx, 版本:1.3
  4. 用户: xxx
  5. 2011-11-13 16:17:31.506 [11747:307] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
  6. *** Call stack at first throw:
  7. (
  8. 0 CoreFoundation 0x344aaed3 __exceptionPreprocess + 114
  9. 1 libobjc.A.dylib 0x33975811 objc_exception_throw + 24
  10. 2 CoreData 0x338f29c1 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 196
  11. 3 CoreData 0x3389253d -[NSManagedObjectContext save:] + 700
  12. 4 0x00045145 -[XMPPCapabilitiesCoreDataStorage setCapabilities:forHash:algorithm:] + 444
  13. 5 0x00042121 -[XMPPCapabilities xmppStream:willSendPresence:] + 232
  14. 6 0x0003ca57 -[XMPPStream sendElement:withTag:] + 342
  15. 7 0x0003c8f5 -[XMPPStream sendElement:] + 28
  16. 8 0x0004592f -[iPhoneXMPP goOnline] + 54

解决办法:
在stackoverflow找到解决办法:http://stackoverflow.com/questions/1091228/i-keep-on-getting-save-operation-failure-after-any-change-on-my-xcode-data-mod

需要修改coredata的persistentstorecoordinator的代理实现方法,添加options,使得当coredata数据模型发生变化时,自动更该合并变化。

代码如下:

view plain
  1. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  2. if (persistentStoreCoordinator != nil) {
  3. return persistentStoreCoordinator;
  4. }
  5. NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"database.sqlite"]];
  6. NSError *error = nil;
  7. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
  8. [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  9. [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
  10. persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
  11. if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
  12. // Handle error
  13. }
  14. return persistentStoreCoordinator;
  15. }

分享到:

读书人网 >移动开发

热点推荐