Tom Redman
Tom Redman

Reputation: 5650

NSPredicate Issue - Not finding data

I have a Core Data model like so:

ArticleCache:
- NSString *link

And I have some a custom object like so:

Article:
- NSString *linkString //NSString version of link
- NSURL *link
- NSString *title

I have an NSArray of Articles (articleList below), and I have a Core Data store with ArticleCaches.

I'm trying to retrieve the ArticleCaches that have a matching link in my articleList:

NSEntityDescription *articleCacheObjectModel = [NSEntityDescription entityForName:@"ArticleCache" inManagedObjectContext:backgroundContext];

// Setup the fetch request  
NSFetchRequest *request = [[NSFetchRequest alloc] init];  
[request setEntity:articleCacheObjectModel];

//search will return all items matching the link
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"link in %@", articleList];
[request setPredicate:searchPredicate];

NSError *error = nil;

NSArray *results = [backgroundContext executeFetchRequest:request error:&error];

When I run this, results is always empty, despite that I know there are matching ArticleCaches in the Core Data store. I know this because if I remove the predicate from the request, I get all the ArticleCaches, and I can print out their respective links and compare them.

Any ideas?

Upvotes: 0

Views: 157

Answers (1)

Martin R
Martin R

Reputation: 540105

The "link" attribute of the "ArticleCache" entity is a string, and in your predicate you try to test if this string is contained in an array of Article objects. That will never match.

You can build an array of string URLs first, e.g.

NSArray *urlList = [articleList valueForKey:@"linkString"];

and then use that in the predicate:

[NSPredicate predicateWithFormat:@"link in %@", urlList];

Upvotes: 2

Related Questions