Re: Store NSPredicate in Core Data
Re: Store NSPredicate in Core Data
- Subject: Re: Store NSPredicate in Core Data
- From: Dave DeLong <email@hidden>
- Date: Sun, 17 Oct 2010 11:51:13 -0600
I would recommend against this approach (saving the predicateFormat string) if you care about the internal structure of the predicate (and I think in this case, you might).
If you have a predicate editor, with the outermost (topmost) predicate being the compound predicate row (any/all/none of the following are true), and the user only chooses a single condition, you'll end up with essentially:
NSPredicate * compound = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObject:[NSPredicate predicateWithFormat:@"condition"]]];
If you save this predicate as-is (using NSKeyedArchiver), then when you'll get it back, you'll be given back an NSCompoundPredicate, which means that when you setObjectValue: on the NSPredicateEditor, you'll end up with the compound row as the first row (as you will probably want).
However, if you choose to save only the predicateFormat of this compound predicate, that predicate format will just be @"condition". So when you recreate it as a predicate, you will end up with an NSComparisonPredicate, *not* an NSCompoundPredicate. When you then take the resulting predicate and setObjectValue: on your editor, the topmost row will not be the compound row.
Put another way:
NSPredicate * simple = [NSPredicate predicateWithFormat:@"1 = 1"];
NSPredicate * compound = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObject:simple]];
NSLog(@"%d", [simple isEqual:compound]); //false
NSLog(@"%d", [[simple predicateFormat] isEqual:[compound predicateFormat]]); //true
Again, this only really matters if you care about the actual structure of the predicate (maintaining the outermost compound predicate), but it's not difficult to imagine why you might.
Either the transformable attribute approach, or the NSKeyedArchiver/NSData attribute approach, are excellent ways to solve this.
Cheers,
Dave
On Oct 17, 2010, at 11:15 AM, Martin Hewitson wrote:
> If it's still of use, I did this with predicateWithFormat: and -description wrapped up in a NSValueTransformer subclass. In other words I store a string representation in the core-data model. I don't know if this is always valid, but it works well for the predicates in my app.
>
> Martin
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden