I have a method which queries Core Data if an object for today already exists.
My code:
CoreDataHelper *cdh = [(MRMedSafeAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
NSManagedObjectContext *context = [cdh context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BMI" inManagedObjectContext:context];
[request setEntity:entity];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
NSDate *today = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSLog(@"patient: %@", patient);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(patient == %@) AND (erstellt_am == %@)", patient, today];
[request setPredicate:pred];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
The data model:
bmi.patient is a To-One relationship with Patient.
I don't understand what is wrong, there is not even a ALL or ANY clause in my predicate.
Can someone enlighten me?
This error indicates that there's two threads trying to access the database. Let's not forget that CoreData is non thread safe.
Check your threads and the access to the database as there will be competition in the threads to access the database.
Hope this helps with your problem. If you are accessing the database from different threads try to use completion handlers or KVO notifications in order to manage the thread access to the database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With