0
votes

when trying to save a managed object with the following configuration in the persistent store coordinator:

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ParallelPhotosModel.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

i get the following error:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "An error occurred while saving." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite, NSAffectedStoresErrorKey=( " (URL: file:///var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite)" ), NSUnderlyingError=0x127335c50 {Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist." UserInfo={NSUnderlyingError=0x12732a670 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite}}}}}, { NSAffectedStoresErrorKey = ( " (URL: file:///var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite)" ); NSFilePath = "/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite"; NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=4 \"The file doesn\U2019t exist.\" UserInfo={NSUnderlyingError=0x12732a670 {Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9FCED2FF-F976-4780-8192-208519C8CD11/Documents/ParallelPhotosModel.sqlite}}}"; }

what is the underlying problem here and how do i avoid it? I ve seen somewhere that a similar error pops up when trying to save inside the App bundle but the storeURL i m using was taken from a tutorial (that is the documents directory).

UPDATE:

to be more specific, I paste where exactly NSManagedObjectContext should allow me to save but it does not:

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    for (DBMetadata *file in metadataContents) {
        NSString *photo_info = [file photo_info];
        if (photo_info != nil) {
            NSNumber *day_taken = [self extractDayFromString: photo_info];
            NSNumber *month_taken = [self extractMonthFromString: photo_info];
            NSNumber *year_taken = [self extractYearFromString: photo_info];
            NSString *db_path = [file path];
            NSManagedObject *parallelPhoto = [NSEntityDescription
                                              insertNewObjectForEntityForName:@"ParallelPhotos"
                                              inManagedObjectContext:context];
            [parallelPhoto setValue:db_path forKey:@"pathInDropbox"];
            [parallelPhoto setValue:day_taken forKey:@"day"];
            [parallelPhoto setValue:month_taken forKey:@"month"];
            [parallelPhoto setValue:year_taken forKey:@"year"];
            NSLog(@"ADDED TO DB: %@ %@ %@", day_taken, month_taken, year_taken);
        }
    }
NSError *error = nil;
if(![context save:&error]){
    NSLog(@"error: %@", error);
2
Can you show the code in applicationDocumentsDirectory - Paul.s
@Paul.s this is the built in method that gets the documents directory - Apostolos Apostolidis
There is no built in method with that selector - Paul.s
That code snippet is not saving changes. Is that the code causing the problem, or is the problem somewhere else in the code that saves changes? - Tom Harrington
the error is output when - (void)saveContext is called. but as you can see from the error and as i expect the store location is configured by the snippet I include above - Apostolos Apostolidis

2 Answers

1
votes

You may want to make sure the directory exists before adding the URL to the persistent store.

[[NSFileManager defaultManager] createDirectoryAtURL:[storeURL URLByDeletingLastPathComponent]
                         withIntermediateDirectories:YES
                                          attributes:nil
                                               error:NULL];

Actually, you may want to add this code to your applicationDocumentsDirectory method before returning the value. If you do that, make sure you properly handle the potential error.

EDIT

Put a breakpoint on the line

NSManagedObjectContext *context = [appDelegate managedObjectContext];

and step through that. Inspect the attributes of the MOC and PSC. You should also inspect the URL held by the persistent stores. Then you should examine the file system to see what is in that directory.

If none of that works, put a breakpoint at the point where the MOC/PSC are initially created and make sure it is created properly. Examine the file system to make sure the store is there.

Then, install an observer on the file and/or file system to be notified anytime it changes, and see if you can catch the file being deleted or moved.

0
votes

So, I ve changed several things in my implementation but what fixed it for me I think is the following line:

[newAddress.managedObjectContext save:nil]

instead of [context save:&error]

which is a managedObjectContext that seems to have been the wrong one. Probably it was some duplicate. So, I conclude it is a good strategy to always use the managedObjectContext owned by the Object that you ve just made changes to it just to be in the safe side.