Reputation: 1387
I have an application that is ultimately a database program. Currently, I am using an NSMutableArray of "element" objects which have a couple dozen pre-defined properties and one property which is a mutable dictionary that is used for user-defined properties (which are defined at run-time).
To do this I use valueForUndefinedKey:
which causes a lookup in the "user fields" mutable dictionary. It all works very well and allows me to build predicates based on both built-in properties as well as user-defined properties.
I am considering moving it to Core Data but can't determine a way to allow the user to define properties (attributes in Core Data-speak). I can create a transformable attribute that is a dictionary which the user could add keys/values to, but there is no way to search it.
I also thought that my built-in properties/attributes could be done in Core Data and then a separate non-Core Data array could hold dictionaries for my user-defined fields (basically the same way it does now). Will this work well?
I have an NSTableView that has to show columns from both built-in fields as well as user-defined fields (using bindings) and I need to be able to build Predicates that encompass both types too.
So for example my elements are "Person" and the built-in attributes are Name, Birthday, Phone Number, etc and then the user can add fields to these such as "Make, Model, Credit Score", etc.
I am not certain that Core Data is a good fit for this when my app has to allow the creation of user-defined fields/properties/attributes, while still keeping them searchable with user-built predicates.
Upvotes: 0
Views: 96
Reputation: 1589
Have you considered defining a entity to store User-Defined key/value pairs? Something like:
This would allow you continue to use predicates to query your data, for instance:
let request = NSFetchRequest<Person>(entityName: "Person")
request.predicate = NSPredicate(format: "userDefined.key == %@", "somekey")
Or queries based on value or data type:
request.predicate = NSPredicate(format: "userDefined.value == %@", "somevalue")
request.predicate = NSPredicate(format: "userDefined.dataType == %@", "String")
You could continue to use a transformable type on UserDefined.value
or a binary type, using the dataType
as a hint to how to transform/decode the data.
Upvotes: 2