Core data. NSPredicate issue using constant literals as names of properties

 ̄綄美尐妖づ 提交于 2020-01-04 12:45:00

问题


I have NSManagedObject with properties. Suppose that one property it is:

@property (retain, nonatomic) NSString *city;

For fetching data form core data I always use NSPredicate. The predicate looks like below:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"city", @"San Francisco"];

But the problem here that the project use key "city" as undefined key in meaning like a constant. So lets suppose that I use key "city" in my app 1000 times, so then I need to replace all this keys with new one if property of NSManagedObject was changed, e.g. to cityName. In this case I need to use predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"cityName", @"San Francisco"];

It is not comfortable. So right now I am using global extern NSString key kCity = @"city"; but in case if the data model changed as well there is a problem, because if property will be changed I will have crash only when run app and console will say me:

'key path city not found in entity <NSSQLEntity...

Which workaround can be here? I thought using something like

NSString * const kCity = myEntity.city.propertyString;

So if property was changed then parser will tell me that project has an error and I can't run app. So after I change property for constant only then I will be able to run app

How can get name of property in NSString. Of course it is just suggestion and maybe someone have another variant.

Thanks


回答1:


You could use mogenerator, one thing it does is to create a struct for attribute and relationship keys that you can use.

// .h
extern const struct MBListAttributes {
    __unsafe_unretained NSString *title;
} MBListAttributes;

// .m
const struct MBListAttributes MBListAttributes = {
    .title = @"title",
};

You can then use MBListAttributes.title to get the correct key.




回答2:


I use this macro-based solution to avoid string literals when I want the name of a key.

It allows you to write key names in this fashion:

[NSPredicate predicateWithFormat:@"%K = %@", Key(YourManagedObjectSubclass, propertyName), @"aValue"];

The compiler will check if YourManagedObjectSubclass has a property of the given name.

But of course, keep in mind that if you change an attribute name in your data model you still need to change it in your NSManagedObject subclass.



来源:https://stackoverflow.com/questions/23473897/core-data-nspredicate-issue-using-constant-literals-as-names-of-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!