1

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!

andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
Gabriel
  • 561
  • 5
  • 9
  • 2
    Best guess is that you're violating the multi-threading restrictions of CoreData and trying to modify CoreData from a background thread. – David Berry Apr 22 '14 at 19:54
  • I thought that too, might setting atomically:YES be it (Im not using any threads). – Gabriel Apr 22 '14 at 19:56
  • You're using AVFoundation, so you're using threads. AVFoundation callbacks are made on background threads. atomically:YES isn't the problem, that just changes how the file is written out (it writes temp, then moves temp to name) – David Berry Apr 22 '14 at 20:01
  • How can I make the save happen in in the main thread then. And thanks for the help! – Gabriel Apr 22 '14 at 20:02
  • 1
    Many different approaches, best bet is probably to search SO for CoreData background and/or dig out the Apple CoreData concurrency documentation which gives a good review of all the approaches. For your case, probably the simplest is going to be `-[NSManagedObjectContext performBlockAndWait:]` – David Berry Apr 22 '14 at 20:06
  • Tried it and the error persists. This is whats really bugging me: [__NSDate localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance – Gabriel Apr 22 '14 at 20:33
  • @David, you should post that as an answer. – paulmelnikow Apr 26 '14 at 03:38

1 Answers1

6

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

Community
  • 1
  • 1
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
  • If you notice, it is set to compare. However you're right in a way, In a view controller in which I do a search based on date, it was set to localizedCaseInsensitiveCompare and this was causing the crash. – Gabriel Apr 23 '14 at 01:58
  • I did notice. In my answer I didn't mention your code. I made it clear that you cannot call `localizedCaseInsensitiveCompare` on an `NSDate`, in direct response to your error written into your question. So in fact I am correct. Happy to have helped!!! – andrewbuilder Apr 23 '14 at 02:09