I have an app which saves text and the date from a UIDatePicker and then shows that note if you got back into that date in the UIDatePicker.
Its works great! Only I have found that setting the UIDatePicker date to today stops CoreData working!
Its only when I run this setDate line does it stop core data from working. The app runs fine without crashing, it just doesn't save any data. If I comment that line out, its works a charm. But I need to have the UIDatePicker on today when the app loads.
I use this when the application starts:
NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now];
This to fetch the note:
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *testEntity = [NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.managedObjectContext];
[fetch setEntity:testEntity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateSaved == %@", datePicker.date];
[fetch setPredicate:pred];
NSError *fetchError = nil;
NSArray *fetchedObjs = [self.managedObjectContext executeFetchRequest:fetch error:&fetchError];
if (fetchError != nil) {
NSLog(@"fetchError = %@, details = %@",fetchError,fetchError.userInfo);
}
noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];
And this to save the note:
NSManagedObject *newDatedText;
newDatedText = [NSEntityDescription insertNewObjectForEntityForName:@"DatedText" inManagedObjectContext:self.managedObjectContext];
[newDatedText setValue:noteTextView.text forKey:@"savedText"];
[newDatedText setValue:datePicker.date forKey:@"dateSaved"];
NSError *saveError = nil;
[self.managedObjectContext save:&saveError];
if (saveError != nil) {
NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
}