EVReflection/Realm
This is the sub specification for a Realm extension for EVReflection
General information
If you have a question and don't want to create an issue, then we can
EVReflection is used in EVCloudKitDao and EVWordPressAPI
In most cases EVReflection is very easy to use. Just take a look at the YouTube tutorial or the section It's easy to use. But if you do want to do non standard specific things, then EVReflection will offer you an extensive range of functionality.
Available extensions
There are extension available for using EVReflection with Realm, XMLDictionairy, CloudKit, Alamofire and Moya with RxSwift or ReactiveSwift
- XML
- CloudKit
- CoreData
- Realm
- Alamofire
- AlamofireXML
- Moya
- MoyaXML
- MoyaRxSwift
- MoyaRxSwiftXML
- MoyaReactiveSwift
- MoyaReactiveSwiftXML
Installation
CocoaPods
Advanced object mapping
This subspec can use all EVReflection features like property mapping, converters, validators and key cleanup. See EVReflection for more information.
Usage
Extend your Realm objects with the EVReflectable protocol
import EVReflection
import RealmSwift
class Person: Object, EVReflectable {
dynamic var name = ""
dynamic var age = 0
dynamic var spouse: Person?
let cars = List<Car>()
}
class Car: Object, EVReflectable {
dynamic var brand = ""
dynamic var name: String?
dynamic var year = 0
}
class PrimitiveListAndOptionalObject: Object, EVReflectable {
let strings = List<String>()
let optionalInt = RealmOptional<Int>()
}
You can then..:
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "TemporaryRealm"))
func testRealmSmokeTest() {
// Create the objects
let wife = Person(json: "{\"name\": \"Jennifer\", \"age\": \"47\", \"cars\": [{\"brand\": \"DeLorean\", \"name\": \"Outatime\", \"year\": 1981} , {\"brand\": \"Volkswagen\", \"year\": 2014}], \"spouse\": {\"name\": \"Marty\", \"age\": \"48\"}}")
// set the circular reference: The spouse of my spouse is me
wife.spouse?.spouse = wife
// You will see _EVReflection_parent_ with the value 1 to indicate that there is a circular reference to it's parent 1 level up.
print("wife = \(wife.toJsonString())")
// Now the object printed using Realm output functionality which just repeats itself until maximum depth is exeeded
print("wife = \(wife)")
// Write objects to the realm
try! realm.write {
realm.add(wife)
}
// Read objects back from the realm
let favorites = ["Jennifer"]
let favoritePeopleWithSpousesAndCars = realm.objects(Person.self)
.filter("cars.@count > 1 && spouse != nil && name IN %@", favorites)
.sorted(byKeyPath: "age")
for person in favoritePeopleWithSpousesAndCars {
print(person.name)
print(person.age)
for car in person.cars {
print("car.name = \(car.name ?? "")")
print("car.brand = \(car.brand)")
print("year = \(car.year)")
}
// Update objects
guard let car = person.cars.first else {
continue
}
print("old car.year = \(car.year)")
try! realm.write {
car.year += 3
}
print("new car.year = \(car.year)")
}
// Delete objects
print("Number of persons in database before delete = \(realm.objects(Person.self).count)")
try! realm.write {
realm.deleteAll()
}
print("Number of persons in database after delete = \(realm.objects(Person.self).count)")
// Thanks! To learn more about Realm go to https://realm.io
}
func testPrimitiveLists() {
let obj = PrimitiveListsObject(json: "{\"strings\":[\"a\",\"b\",\"c\"],\"optionalInt\":3}")
XCTAssertEqual(obj.strings.count, 3, "The strings array should have 3 values")
XCTAssertEqual(obj.optionalInt.value, 3, "The optional int should have been set to 3")
print("The object: \(obj)")
}