xcode ios:将核心数据包含到旧项目后崩溃

Posted

技术标签:

【中文标题】xcode ios:将核心数据包含到旧项目后崩溃【英文标题】:xcode ios : crashing after including coredata to old project 【发布时间】:2012-06-13 14:37:06 【问题描述】:

我有一个正在修改的工作项目,

尝试使用 coredata 后崩溃,

项目之前没有coredata,所以添加了,

与之前的基础 sdk [number??] 一样,applicationDocumentsDirectory 返回一个 NSString,而不是 URL,

我已将其更改为工作?但在尝试使用 coredata 时崩溃,

#import "QRGenAppDelegate.h"

//#import "QRGenViewController.h"

#import "MainViewController.h"


@implementation QRGenAppDelegate


//@synthesize window; //viewController

@synthesize window = _window;
@synthesize navigationController = _navigationController;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (void)dealloc

    [__managedObjectModel release];
    [__managedObjectContext release];
    [__persistentStoreCoordinator release];
    [_window release];
    [_navigationController release];

    //[viewController release];
    [super dealloc];


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    MainViewController *menuVC= [[[MainViewController alloc]init]autorelease];

    self.navigationController = [[UINavigationController alloc]initWithRootViewController:menuVC];

    //self.window.rootViewController = menuVC;

    self.navigationController.navigationBar.hidden = YES;

    [self.window addSubview:self.navigationController.view];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];



    return YES;



//CD

//Explicitly write Core Data accessors
- (NSManagedObjectContext *) managedObjectContext 
    if (__managedObjectContext != nil) 
        return __managedObjectContext;
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) 
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator: coordinator];
    

    return __managedObjectContext;


- (NSManagedObjectModel *)managedObjectModel 
    if (__managedObjectModel != nil) 
        return __managedObjectModel;
    
    __managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];

    return __managedObjectModel;


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

    if (__persistentStoreCoordinator != nil) 
        return __persistentStoreCoordinator;
    


    //mi solucto!!

    //NSURL *storeURL= [NSURL URLWithString:[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ClientOcci.sqlite"]];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"db.sqlite" ofType:nil];

    NSURL *storeURL = [NSURL URLWithString:filePath];

   // NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ClientOcci.sqlite"];
    //NSURL *storeURL = [[self applicationDocumentsDirectory]    URLByAppendingPathComponent:@"ChildCare_v02.sqlite"]; //actual SDK style for blank db

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 


        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
        

    return __persistentStoreCoordinator;



#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
+ (NSString*)applicationDocumentsDirectory 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;

//CD 


- (void)applicationWillResignActive:(UIApplication *)application



- (void)applicationDidEnterBackground:(UIApplication *)application



- (void)applicationWillEnterForeground:(UIApplication *)application



- (void)applicationDidBecomeActive:(UIApplication *)application



- (void)applicationWillTerminate:(UIApplication *)application





@end

它崩溃了:

__managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];

EXC_BAD_ACCESS

那么我是如何在项目设置中尝试制作基本 SDK 5.1 但仍然崩溃的,

为什么我的项目不喜欢 URL 作为返回方法? 如何解决? 如果我将其更改为较新的 SDK,是否会以其他方式制动?

编辑>> 我已经尝试过@ierror 的建议,

添加到我的 rootVC:

if (managedObjectContext == nil)  
        managedObjectContext = [(QRGenAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        NSLog(@"After managedObjectContext: %@",  managedObjectContext);

但是现在在appDelegate中崩溃了,

- (NSManagedObjectModel *)managedObjectModel 
    if (__managedObjectModel != nil) 
        return __managedObjectModel;
    
    __managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; // crashes here!!!

    return __managedObjectModel;

怎么办?

非常感谢!

【问题讨论】:

一个愚蠢的问题,我知道,但是是否有一个有效的模型可供代码提取(即 xcdatamodeld 文件)?尝试合并模型包时它崩溃的面孔让我认为那里可能存在问题。另外,持久存储是否有效,我的意思是在它崩溃之前调用NSLog(@"store %@", __persistentStoreCoordinator); 是否会给出非零值? 【参考方案1】:

关于堆栈溢出请参考这个问题

Core data error iPhone

它会解决你的问题

【讨论】:

您好,谢谢,我尝试了您的建议,但仍然崩溃,请查看编辑,谢谢! 您是否尝试过这些说明:wiresareobsolete.com/wordpress/2009/12/…

以上是关于xcode ios:将核心数据包含到旧项目后崩溃的主要内容,如果未能解决你的问题,请参考以下文章

XCode 6项目在ios 7.1上segue后崩溃

UITableView - 核心数据应用程序中的表视图崩溃,因为删除后行数无效

XCode 5 核心数据模型崩溃 [关闭]

Xcode 6 Beta 在 iPad 7.1 上运行时使用核心数据使应用程序崩溃

iOS 10 应用程序因试图访问隐私敏感数据而崩溃

IOS/xcode:将核心数据从应用委托移动到模型类