Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Predicate variable substitution



I'm trying to write a fetch request in XCode that will load all objects with a specified name value. As in, the type has an attribute "name" and I want to fetch based on that attribute. The problem I'm having is that I can't get any fetch request using substitution to work. Here's my code:

// First try test predicate substitution.
    NSPredicate *predicate =
        [ NSPredicate predicateWithFormat: @"name like '$THE_NAME'" ];
    NSLog( @"unsub predicate = %@", predicate );
    NSPredicate *subPredicate = [ predicate
        predicateWithSubstitutionVariables: [ NSDictionary
            dictionaryWithObject: DEFAULT_NAME forKey: @"THE_NAME" ] ];
    NSLog( @"sub predicate = %@", subPredicate );

    NSManagedObjectModel *mom = [ self managedObjectModel ];
    NSManagedObjectContext *context = [ self managedObjectContext ];
    NSError *error = nil;

// Now try to use the fetch request saved in XCode.
NSFetchRequest *fetchByNameRequest = [ mom
fetchRequestFromTemplateWithName: @"fetchDescriptionByName"
substitutionVariables: [ NSDictionary
dictionaryWithObject: DEFAULT_NAME forKey: @"THE_NAME" ] ];
NSLog( @"fetchByNameRequest = %@", fetchByNameRequest );
NSArray *results = [ context executeFetchRequest: fetchByNameRequest error: &error ];
NSLog( @"fetchByNameRequest found objects: %@", results );
NSLog( @"error = %@", error );


// Finally, do it all by hand.
NSFetchRequest *req = [ [ [ NSFetchRequest alloc ] init ] autorelease ];
NSEntityDescription *entity = [ NSEntityDescription entityForName: @"StaticDescription"
inManagedObjectContext: context ];
[ req setEntity: entity ];
NSArray *moreResults = [ context executeFetchRequest: req error: &error ];
NSLog( @"coded fetch = %@", req );
NSLog( @"coded fetch found objects: %@", moreResults );
NSLog( @"error = %@", error );


And the results are:
2005-10-29 04:26:49.533 NewApp[3120] unsub predicate = name LIKE "$THE_NAME"
2005-10-29 04:26:49.859 NewApp[3120] sub predicate = name LIKE "$THE_NAME"
2005-10-29 04:26:50.366 NewApp[3120] fetchByNameRequest = <NSFetchRequest: 0x36dd00> (entity:Description; predicate: (name LIKE "$THE_NAME"); sortDescriptors: (null); limit: 0)
2005-10-29 04:26:51.215 NewApp[3120] fetchByNameRequest found objects: ()
2005-10-29 04:26:51.765 NewApp[3120] error = (null)
2005-10-29 04:26:52.965 NewApp[3120] coded fetch = <NSFetchRequest: 0x3509b0> (entity: StaticDescription; predicate: ((null)); sortDescriptors: (null); limit: 0)
2005-10-29 04:26:53.310 NewApp[3120] coded fetch found objects: (
<NSManagedObject: 0x36fe00> (entity: StaticDescription; id: 0x387450 <x-coredata://1ABEEAE0-9DA3-4A43-886E-FCFCE362B0E3/ StaticDescription/p102> ; data: <fault>)
)
2005-10-29 04:26:53.997 NewApp[3120] error = (null)


My problem is that predicate substitution for variables doesn't seem to work, nor does the XCode substitution using fetchRequestFromTemplateWithName:substitutionVariables:. The predicate for fetchDescriptionByName in XCode reads:

    name LIKE "$LIB_NAME"

There are a few things that come to mind which could be affecting this. This function is being called from the Application Delegate's applicationWillFinishLaunching: method, so maybe some things are ready to be called at this point? Also, in my data model I've got an abstract "Description" entity, and two concrete sub-entities "StaticDescription" and "DynamicDescription". The fetch request is defined on the abstract "Description" class because I'd like to get all Descriptions, regardless of their exact sub-type, but in this case, I know the type I'm looking for is a "StaticDescription". The hand coded fetch request works with both the abstract and concrete entities.

If anyone has some insight into why the NSPredicate substitution isn't working for me, or why the XCode fetch template doesn't work when a hand coded NSFetchRequest does, I'd appreciate your help. Thanks!

Mark
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.