Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Le Xuan Quynh lexuanquynh

🎯
Focusing
View GitHub Profile
View query.swift
let coffeDrinks = repository.queryAll(returningClass: CoffeeDrink.self)
let cofferRate = coffeDrinks!.filter { $0.rating == 4 }
View savedata.swift
let data = "{\"name\": \"Tennis ball\"}".data(using: .utf8)!
repository.save(saveClass: DogToy.self, jsonData: data, update: false) { result in
switch result {
case .success:
break
case .failure:
break
}
}
View saveentities.swift
let aDog = Dog(value: ["name": "Lulu", "age": 3])
let anotherDog = Dog(value: ["name": "Nana", "age": 2])
// Instead of using pre-existing dogs...
let aPerson = Person(value: [123, "Jane", [aDog, anotherDog]])
// ...we can create them inline
let anotherPerson = Person(value: [456, "Jane", [["Buster", 5], ["Buddy", 6]]])
repository.save(entities: [aPerson, anotherPerson], update: true) { result in
switch result {
View repositoryImplement.swift
let repository = PetRepository()
repository.save(entity: dog, update: false) { result in
switch result {
case .success:
// Handle success here
print("Handle success here")
case .failure:
print("Handle failed here")
}
}
View Person.swift
import RealmSwift
class Person: Object {
@Persisted(primaryKey: true) var id = 0
@Persisted var name = ""
// To-many relationship - a person can have many dogs
@Persisted var dogs: List<Dog>
// Inverse relationship - a person can be a member of many clubs
@Persisted(originProperty: "members") var clubs: LinkingObjects<DogClub>
}
View PetRepository.swift
import RealmSwift
import QRealmManager
class PetRepository: DatabaseConfigurable {
var realmMemoryType: RealmMemoryType {
return .inStorage
}
var schemaName: String? {
return "PetSchema"
View package.py
Thêm bng dòng sau:
https://github.com/lexuanquynh/QRealmManagerPackage.git
Sau đó import:
import QRealmManagerPackage
View pod.py
pod 'QRealmManager'
sau đó import thư vin:
import QRealmManager
View migrationBlock.swift
var migrationBlock: MigrationBlock? {
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
// Rename the "name" property to "yourName".
// The renaming operation should be done outside of calls to `enumerateObjects(ofType: _:)`.
migration.renameProperty(onType: MigrationSample.className(), from: "age", to: "yearsSinceBirth")
// migration.renameProperty(onType: MigrationSample.className(), from: "name", to: "yourName")
}
// The enumerateObjects(ofType:_:) method iterates over
View objectTypes.swift
var objectTypes: [Object.Type]? {
return [DogToy.self, Dog.self, DogClub.self, Person.self]
}