开发者

CoreData's NSFetchrequest: Provide classname and propertynames moredynamically

In CoreData, when you want to query for an object, you have to specify the name of the entity and the names of the properties in strings like that:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

fetchRequest.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"Name=%@ AND Forename=%@", name, vorname];

I don't really like this approach, because in doing so, the IDE can't help me in scenarios like renaming one of the properties or renaming the classname.

Using this code-block, I made my call a little more dynamic by getting the classname by introspection:

NSString *classname = [[Person class] description];    
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

fetchRequest.entity = [NSEntityDescription entityForName:classname inManagedObjectContext:context];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"Na开发者_如何学编程me=%@ AND Forename=%@", name, vorname];

Is there any way to get the name of the properties (Name and Forename) the same way? I don't know how to handle properties dynamically.

Do you get my point? I'd be happy, if someone could point me into the right direction.


I've used plists in the resource bundle in this way, sort of-- basically, create an NSDictionary that maps keys that are your parameters to the names of the properties on the object at hand, then just name it MyClassname.plist for easy loading into memory. You'd have to write one up for every class you want this for, though. (and edit them if you change the property names)

Not sure if there is an easier way.

And, IIRC, you sub in the keys as so:

[NSPredicate predicateWithFormat:@"%k=%@",[plist objectForKey:@"param1"],[personObject valueForKey:[plist objectForKey:@"param1"]]]


I'm not sure I see a big advantage of entityForName:[[Person class] description] over entityForName:@"Person" (or maybe entityForName:PersonEntityName where PersonEntityName is just a constant). It works fine provided that the name of the class that represents an entity is the same as the name of the entity, but that's often not the case. It's quite common to use plain old NSManagedObject for simple entities, and that obviously doesn't match the entity name.

Likewise, if you're creating a fetch request, the names of the properties that you're interested in are usually already determined and unlikely to change, so specifying them by name is usually the easiest thing to do. You can get an array of properties from the entity description, but how will you know which property to use if you don't already know its name? And if you know the name of the property, there's not much sense in retrieving the property description just so you can get its name. ;-)

There are times when it's helpful to leave the entity and properties undetermined until the application runs, such as when you want to let the user specify the parameters of the search. That's clearly not the case in the example you gave, though, because you've given the format of the predicate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜