开发者

IOS CoreData - Cant load NSPersistentStoreCoordinator

ive got an xcode4 project named TestProject that im trying to add CoreData to. I've added a data model (named TestDataModel) with a few entities and created NSManagedObject classes for the entities.

My problem is i cant load the Persistent Store

if(self.managedObjectContext == nil){
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        NSURL* storeUrl = [NSURL fileURLWi开发者_运维百科thPath:[basePath stringByAppendingPathComponent: @"TestProject.sqlite"]];
        NSError* errors;

        NSPersistentStoreCoordinator* persistentStoreCoordinator = 
                           [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel 
                                                                     mergedModelFromBundles:nil ]];

        if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                     configuration:nil 
                                                               URL:storeUrl 
                                                           options:nil 
                                                             error:&errors])
        {
            NSLog(@"Error loading persistent store: %@", [errors localizedDescription]);
        }

        self.managedObjectContext = [[NSManagedObjectContext alloc] init];
        [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

    }

I Keep getting the error:

Error loading persistant store: The operation couldn’t be completed. (Cocoa error 258.)

Please note i'm also running this in the simulator.

Thanks in advance.


you are using the wrong directory.

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^

change this into

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                                     ^^^^^^^^^^^^^^^^^^^

The NSDocumentDirectory is created by iOS, but the NSDocumentationDirectory directory isn't.


Figured it out. Didn't realise i needed to create the database

Below code works

if(self.managedObjectContext == nil){

        NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"TestProject.sqlite"];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath:storePath]) {
            NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"TestProject" ofType:@"sqlite"];
            if (defaultStorePath) {
                [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
            }
        }

        NSURL *storeUrl = [NSURL fileURLWithPath:storePath];


        NSError* errors;
        NSPersistentStoreCoordinator* persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];

        if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&errors])
        {
            NSLog(@"Error loading persistent store: %@", [errors localizedDescription]);
        }

        self.managedObjectContext = [[NSManagedObjectContext alloc] init];
        [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

    }

Youll need this aswell

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜