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?
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
Articleobjects. That will never match.You can build an array of string URLs first, e.g.
and then use that in the predicate: