Skip to content

Instantly share code, notes, and snippets.

View lexuanquynh's full-sized avatar
👋
coding ...

Quynh lexuanquynh

👋
coding ...
View GitHub Profile
let coffeDrinks = repository.queryAll(returningClass: CoffeeDrink.self)
let cofferRate = coffeDrinks!.filter { $0.rating == 4 }
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
}
}
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 {
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")
}
}
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>
}
import RealmSwift
import QRealmManager
class PetRepository: DatabaseConfigurable {
var realmMemoryType: RealmMemoryType {
return .inStorage
}
var schemaName: String? {
return "PetSchema"
Thêm bằng dòng sau:
https://github.com/lexuanquynh/QRealmManagerPackage.git
Sau đó import:
import QRealmManagerPackage
pod 'QRealmManager'
sau đó import thư viện:
import QRealmManager
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
var objectTypes: [Object.Type]? {
return [DogToy.self, Dog.self, DogClub.self, Person.self]
}