Question

Using core data, I created a fetch which should always return a single object (based on a specific predicate) but somehow it is returning multiple objects. I'm wondering if there is a flaw in my understanding of how core data predicates/comparisons work.

Overview of relevant part of my model (for an email client)

Entity: Thread
Relevant Attributes: thread.folder, thread.threadId

Entity: Folder
Relevant Attributes: folder.threads, folder.name

In core data, different threads can have the same thread id, but should always have a different folder.

Example: 

Thread 1: thread.threadId = 12345, thread.folder.name = @"inbox"
Thread 2: thread.threadId = 12345, thread.folder.name = @"important"

So, I would assume that if I fetch a thread that has BOTH a threadId of 12345 AND folder.name of "inbox", I should only get 1 thread. But I get 2 in the above circumstance.

Here is how I am fetching from core data:

- (Thread *)threadForFolder:(Folder *)folder threadId:(uint64_t)threadId
{
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
 [request setEntity:[Thread entityDescriptionInManagedObjectContext:self.managedObjectContext]];
 [request setPredicate:[NSPredicate predicateWithFormat:@"threadId == %llu && folder == folder", threadId, folder]];

 NSArray *results = [self.managedObjectContext executeFetchRequest:request error:nil];
 //ARRAY RETURNS TWO VALUES!?

 //...more stuff
}

Why does this not work?

Was it helpful?

Solution

I think you just have a typo. You just say folder == folder instead of using the parameter you are passing in. So of course, folder always equals folder.

 [request setPredicate:[NSPredicate predicateWithFormat:@"threadId == %llu && folder == %@", threadId, folder]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top