A framework for creating and executing type-safe queries in Swift. Inspired by QueryKit and AlecrimCoreData.
- Core Data Compatibility
- Realm Compatibility
- Automatic attribute generator for
Realm. - Complete Documentation
To use QueryGenie, you first need to add extensions to your model objects providing attributes for all queryable properties on your models. For Realm models, this can be done simply using the RealmGenerator. A generator for CoreData models is planned for future release.
An extension for a User model might look as follows:
extensionUser{staticvarname:Attribute<String>{returnAttribute("name")}staticvarage:Attribute<Int>{returnAttribute("age")}}This allows us to create predicates using our model types.
letnamePredicate:NSPredicate=User.name =="Kyle"letagePredicate:NSPredicate=User.age >25These predicates can be used to filter, sort, and execute queries on Queryable entities.
For Realm:
letrealm=try!Realm()letusers:Results<User>= realm.objects(User.self).filter{ $0.name =="Kyle"}.filter{ $0.age >25}.sortedBy(ascending:true){ $0.name }.sortedBy(ascending:false){ $0.age }For CoreData:
letcontext=NSManagedObjectContext(concurrencyType:.mainQueue)letusers=Table<User>(context: context).filter{ $0.name =="Kyle"}.filter{ $0.age >25}.sortedBy(ascending:true){ $0.name }.sortedBy(ascending:false){ $0.age }This library support many other advanced querying capabilities including sum, min, max, average, any, none, and more. For more information, please check out the documentation.
Model objects that have a primary key may provide an attribute for the primary key in an extension to support querying unique objects.
For Realm, if your object returns a value for primaryKey, all you need to do is make your object conform to UniqueIdentifiable and provide the value type of your primary key. This extension will be generated for you if you use RealmGenerator.
extensionUser:UniqueIdentifiable{publictypealiasUniqueIdentifierType=String}For CoreData, make your object conform to UniqueIdentifiable and provide a static attribute for the primaryKey with the name of the primary key property on your entity.
extensionUser:UniqueIdentifiable{staticvarprimaryKey:Attribute<String>{returnAttribute("id")}}A Command Line Tool to make automatic attribute generation easier is planned for future release.
QueryGenie provides a helper class to automatically generate attribute extensions for all of your Realm objects. A generator for CoreData is planned for future release.
To use the RealmGenerator, inlcude the QueryGenieRealmGenerator pod in a new test target in your Xcode project.
In that test target, you can create a test that will generate your extensions.
import XCTest
import QueryGenieRealmGenerator
import Realm
classGenerateAttributes:XCTestCase{func test_generateAttributes(){letpath="/Users/AnthonyMDev/MyProject/Models/QueryGenieExtensions"leturl=URL(fileURLWithPath: path, isDirectory:true)letconfig=RLMRealmConfiguration.init()
config.deleteRealmIfMigrationNeeded =truetry?RealmGenerator.generateAttributeFiles(for:RLMRealm(configuration: config).schema, destination: url)}}CocoaPods is a dependency manager for Cocoa projects.
To integrate QueryGenie into your Xcode project using CocoaPods, specify it in your Podfile:
source'https://github.com/CocoaPods/Specs.git'platform:ios,'8.0'use_frameworks!pod'QueryGenie/Realm','~> 1.0' // For use with Realm modelspod 'QueryGenie/CoreData', '~> 1.0' //ForusewithCoreDatamodelsThen, run the following command:
$ pod install