Core Data Error - NSDate localizedCaseInsensitiveCompare: unrecognised selector sent to instance

人盡茶涼 提交于 2019-12-08 09:07:34

问题


I have searched for the last several hours but have yet to find an answer. I implemented an AVFoundation camera, Im saving the image data to disk and storing only the path in core data. Everything works fine but after a random number of taken photos I get this error:

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSDate localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance

This is my fetch request code:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notebook = %@", notebookItem];
[request setPredicate:predicate];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dateTaken"
                                                              ascending:NO
                                                               selector:@selector(compare:)]];

And here is how I save the data in a separate class:

//I create a photo Object to save to core data and set properties like dateTaken and tittle

//Here is where I create the path name
NSDate *dateString = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *path = [formatter stringFromDate:dateString];

//Now I save to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:path];
NSLog(@"path: %@", path);
[[[self captureManager] ImageDataObject] writeToFile:filePath atomically:YES];

photo.imagePath = filePath;

[notebookItem addPhotosObject:photo];
[self.SharedDocumentHandlerInstance saveDocument];

I tracked down the point of crash and it occurs in the line of code where I save the document (the last one above) but maybe the fetch request is the one that causes it. I also set self.fetchedResultsController.delegate = nil in my table. And again, this error occurs after a random number of photos is taken. Thanks for any help in advance!


回答1:


Your problem is that you cannot call localizedCaseInsensitiveCompare: on an NSDate. You need to change that to compare:. Check through the Apple documentation on NSDate.

The method localizedCaseInsensitiveCompare: requires an NSString (Apple documentation).

This SO Question may also be helpful.

Also Table 1 in this web page Creating and Using Sort Descriptors



来源:https://stackoverflow.com/questions/23228849/core-data-error-nsdate-localizedcaseinsensitivecompare-unrecognised-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!