核心数据崩溃:[__NSArrayM insertObject:atIndex:]: 对象不能为零 [英] Core Data Crash: [__NSArrayM insertObject:atIndex:]: object cannot be nil

查看:19
本文介绍了核心数据崩溃:[__NSArrayM insertObject:atIndex:]: 对象不能为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现 CoreData 来存储一个小型数据库.

I am trying to implement CoreData in my application to store a small database.

这里是我的实现:

AppDelegate.h

AppDelegate.h

#import <UIKit/UIKit.h>
#import "FavoritosViewController.h"
#import <CoreData/CoreData.h>

@interface XXX : NSObject <UIApplicationDelegate>{

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;



}
- (NSString *)applicationDocumentsDirectory;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

AppDelegate.m

AppDelegate.m

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

    FavoritosViewController *global=[[FavoritosViewController alloc]init];

    global.managedObjectContext=[self managedObjectContext];

    .
    .
    .
    }

        - (void)applicationWillTerminate:(UIApplication *)application
    {
        NSError *error = nil;
        if (managedObjectContext != nil) {
            if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
                /*
                 Replace this implementation with code to handle the error appropriately.

                 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
                 */
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            } 
        }

    }

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}


/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FavoritosDatabase.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible
         * The schema for the persistent store is incompatible with current managed object model
         Check the error message to determine what the actual problem was.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}




- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

我还有一个 xcdatamodeld,其中包含带有它们的属性的Event"实体,以及来自它的 Event.h、Event.m.

I have also a xcdatamodeld with "Event" entity with theirs attributes, and Event.h, Event.m from it.

在 FavoritosViewController 中我也有所有的方法,但问题在这之前出现了.

In FavoritosViewController I have also all the methods, but the problem comes before all that.

它来了

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

应用崩溃并显示以下内容:

The app crash and it appears the following:

* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:*-[__NSArrayM"insertObject:atIndex:]: 对象不能为 nil'

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

有什么想法吗???谢谢!!!

Any ideas??? Thanks!!!

推荐答案

当我的代码运行 [NSManagedObjectModel mergeModelFromBundles:nil] 时,我遇到了类似的问题,并显示了相同的错误消息.这是在 xCode 意外崩溃之后发生的.即使当我恢复到我的代码的已知良好版本时,由于某种损坏,我仍然遇到相同的错误.

I had a similar problem with the same error message when my code ran [NSManagedObjectModel mergedModelFromBundles:nil]. This happened after xCode crashed on me unexpectedly. Even when I reverted back to a known good version of my code, I still had the same error that was due to some sort of corruption.

经过多次试验,我可以通过退出 Xcode 和 iPhone 模拟器解决问题,然后从以下目录中删除所有文件:

After much experimenting, I was able to solve the problem by exiting Xcode and the iPhone simulator, and then deleting all files from the following directories:

$ cd /Users/john/Library/Developer/Xcode/DerivedData

$ rm -R -f ./(folder corresponding to my project name)

$ cd /Users/john/Library/Application Support/iPhone Simulator/5.0/Applications

$ rm -R *

这能够清除模拟器损坏的临时文件和状态,错误消失了.

This was able to clear the corrupted temporary files and state for the simulator, and the error disappeared.

这篇关于核心数据崩溃:[__NSArrayM insertObject:atIndex:]: 对象不能为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆