Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

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?

like image 433
mrd Avatar asked Feb 27 '14 23:02

mrd


1 Answers

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.

like image 70
artud2000 Avatar answered Oct 27 '22 01:10

artud2000