pbattisson
pbattisson

Reputation: 1240

Error trying to access iOS store

All,

I have the following code in my project:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }
    NSError *error = nil;


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

    NSURL *storeURL = [NSURL fileURLWithPath:storePath];


    __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;
}

- (NSString *)applicationDocumentsDirectory
{
    NSError *err = nil;
    return [NSString stringWithContentsOfURL:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] encoding:NSUTF8StringEncoding error:&err];
}

And I am receiving an error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

What I don't understand is why this is returning s nil? I have referenced a book for this code and it seems to think it should work all ok. Any ideas?

Thanks in advance

Paul

Upvotes: 0

Views: 172

Answers (1)

Alfonso
Alfonso

Reputation: 8492

Your -applicationDocumentsDirectory method looks very strange to me. Instead of returning the path of the documents dir, you try to actually return the contents of this directory (which is probably not possible to read, hence it will return nil).

Try replacing the method with

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                   inDomains:NSUserDomainMask] lastObject];
}

Upvotes: 1

Related Questions