small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
10 Jul 2010, 15:20
Brad Gibbs (7 posts)

I’m trying to create code similar to a post on CIMG re iTunes search field. I’ve looked through the NSPredicate Programming Guide, the Core Data book and numerous ‘net posts, with no success. I’m getting an NSFaultingMutableSet error, which, I think, means that a fault isn’t firing, which means the app doesn’t have access to the data in the relationship, so, it can’t search through the attributes on the related entities. I’ve tried pre-fetching relationships, but, that doesn’t work, either (or, rather, I must not be doing it correctly).

Part of my app is an image library. I have an ImageResource entity to track basic information on each image imported into the library, including its URL. I have another entity, TagForResource with a single string attribute, tagString. An image can have multiple tags and each tag can be related to multiple images.

I have a NSSearchField. Text entered into the search field should build a predicate to filter the images in my array controller. I can filter for attributes on the ImageResource, but, when I try to include:

@”ANY tags.tagString like %@”, searchText

I get:

[<_NSFaultingMutableSet 0x2000992e0> addObserver:forKeyPath:options:context:] is not supported. Key path: tagString

Initially, I thought I needed to fire the fault to retrieve the TagForResource objects, so, I added the following to the beginning of my searchFieldChanged method:

	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"ImageResourceP" inManagedObjectContext:self.managedObjectContext];
	[request setEntity:entity];
	NSArray *relKeys = [NSArray arrayWithObjects:@"tags", nil];
	[request setRelationshipKeyPathsForPrefetching:relKeys];
	NSError *error = nil;
	NSArray *relationships = [self.managedObjectContext executeFetchRequest:request error:&error];

but, that didn’t help.

I’ve also tried wiring it through IB, but I get the same results. How can I filter images based on their tag strings?

Thanks.

Brad

Generic-user-small
11 Jul 2010, 01:14
Brad Gibbs (7 posts)

I gave up on this for the moment and used a hack to get nearly the same as what I was looking for. All of my model objects are custom subclasses of NSManagedObject. I’ve found it’s easier to keep custom methods in categories on these custom subclasses, so that when I need to delete them and start again, I don’t lose the custom methods or have to copy and paste them.

Anyway, I added a method to the category on my ImageResourceP that returns an array of all of the tags in that image object using NSArray’s componentsJoinedByString. I think the result is the same, but I still wish I could solve the underlying problem.

Avatarsmall_pragsmall
13 Jul 2010, 05:07
Marcus S. Zarra (284 posts)

The issue is that you have a many to many relationship. That tends to play havoc when you try and search across it. You might want to explore using a subquery in your predicate but to be honest I have never tried it with a search field.

You must be logged in to comment