I am developing an app that uses Core Data. In iOS 5, I don’t have any problem.
However, in iOS 4, when I try to save any information I get an error message (more exactly: Cocoa error 134030, and “No such file or directory”). When I look in the documents folder, the SQL file is not there. I already debugged this, and none of these methods there cause errors or nil pointers.
The strangest part: the method addPersistentStoreWithType doesn’t return any error, but at the same time, the sqlite file is not created.
In one line, my app is not creating the sql file in the documents folder. Again, this happens only in iOS 4.
Why does my app not create the sqlite file on iOS 4?
//Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Practi" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
if (__managedObjectModel == nil)
{
NSLog(@"NSManagedObjectModel in AppDelegate == nil");
}
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyAppDataBase.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(@"APPDELEGATE Unresolved error %@, %@", error, [error userInfo]);
}
return __persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory
{
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
return [NSURL fileURLWithPath:docsDirectory];
}
I discovered what solved the problem:
When each method above written was called, I was creating again the objects, instead of checking if it already existed to return. So, as the code is now, it is working also in iOS 4.