Need some help with fetch requests
-
Hi All,
Today I'm working with NSFetchRequests for the first time and I need
some help learning how to filter down the initial set of managed
objects that I want to search against. My application is a sort of
message viewer/editor. A user can open up multiple files, each
containing some number of messages. Messages themselves are broken
down into any number of fields. So my managed object model follows
that same type of hierarchy..
Entity: File
Relationship: Messages
Entity: Message
Relationship: Fields
Entity: Field
Property: Value (string)
When a user performs a search, the search will be performed on the
"Field" entity's "Value" property. I can make this work great with an
NSFetchRequest. My issue is that the search is searching all Field
objects, even if they are owned by a different file. I want to be
able to search just the fields in the currently selected file, rather
than all files that are currently open in the application. Hope that
makes sense. My initial thought was to add more criteria to my
predicate to help narrow down the search base, but I'm not sure how
the predicate's criteria could traverse relationships. Thanks in
advance for the assistance.
Regards,
Carter -
On Feb 1, 2008, at 12:03 PM, Carter R. Harrison wrote:> Today I'm working with NSFetchRequests for the first time and I need
> some help learning how to filter down the initial set of managed
> objects that I want to search against.> Entity: File
> Relationship: Messages
I assume you actually mean "messages" given that properties of objects
in Objective-C start with lower-case letters. :)> My initial thought was to add more criteria to my predicate to help
> narrow down the search base, but I'm not sure how the predicate's
> criteria could traverse relationships.
Magic. :)
Do all of your relationships have appropriate inverses? If not, they
should. Then you could just use a predicate like this:
NSPredicate *fieldSearchPredicate = [NSPredicate predicateWithFormat:
@"(message.file IN %@) AND (value = %@)", files,
requestedFieldValue];
Use that in a fetch request on the Field entity and it should just
work. "files" should be an NSArray or NSSet of instances of your File
entity, or NSManagedObjectID instances representing instances of your
File entity.
-- Chris -
On Feb 1, 2008, at 3:36 PM, Chris Hanson wrote:> On Feb 1, 2008, at 12:03 PM, Carter R. Harrison wrote:
>
>> Today I'm working with NSFetchRequests for the first time and I
>> need some help learning how to filter down the initial set of
>> managed objects that I want to search against.
>
>> Entity: File
>> Relationship: Messages
>
> I assume you actually mean "messages" given that properties of
> objects in Objective-C start with lower-case letters. :)
You assumed correctly. Thanks for making a note of this though.>
>
>> My initial thought was to add more criteria to my predicate to help
>> narrow down the search base, but I'm not sure how the predicate's
>> criteria could traverse relationships.
>
> Magic. :)
>
> Do all of your relationships have appropriate inverses? If not,
> they should. Then you could just use a predicate like this:
>
> NSPredicate *fieldSearchPredicate = [NSPredicate predicateWithFormat:
> @"(message.file IN %@) AND (value = %@)", files,
> requestedFieldValue];
>
> Use that in a fetch request on the Field entity and it should just
> work. "files" should be an NSArray or NSSet of instances of your
> File entity, or NSManagedObjectID instances representing instances
> of your File entity.
>
Excellent! I had already made inverse relationship so your answer
should work. I'm not sure why I couldn't figure that out, but I do
appreciate it!> -- Chris
>


