Search code examples
iosios5core-dataios6

Storing file path in core data throws exception


I am trying to store the file path of files that are in the device's documents folder in a core data entity, it is inserting properly but when i am trying to fetch records using a predicate on the path attribute it throws me an exception.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the
 format string "filePath==/Users/****/Library/Application Support/iPhone Simulator/5.1/Applications
/8C1B07FD-E372-4CD8-9A02-FDA321ECE629/Documents"'

The attributes are properly stored in Core Data DB.

Code to Fetch out.

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filePath==%@",Path]];
    NSSortDescriptor *sortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
    fetchRequest.sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"File"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSArray *fetchedObjects = [self.managedObjectContext
                               executeFetchRequest:fetchRequest error:nil
                               ];

Solution

  • Maybe the file path contains characters, such as white spaces, that prevent proper parsing. Try this:

    [NSPredicate predicateWithFormat:@"filePath = '%@'", path];