Update playground samples to use Field properties instead of old syntax

This commit is contained in:
John Estropia
2020-12-07 14:08:05 +09:00
parent f985828f3b
commit 668b5ad606
4 changed files with 64 additions and 30 deletions

View File

@@ -6,14 +6,24 @@ PlaygroundPage.current.needsIndefiniteExecution = true
/// Model Declaration ===== /// Model Declaration =====
class Animal: CoreStoreObject { class Animal: CoreStoreObject {
let species = Value.Required<String>("species", initial: "Swift")
let master = Relationship.ToOne<Person>("master") @Field.Stored("species")
let color = Transformable.Optional<UIColor>("color", initial: .orange) var species: String = "Swift"
@Field.Coded("color", coder: FieldCoders.NSCoding.self)
var color: UIColor = .orange
@Field.Relationship("master")
var master: Person?
} }
class Person: CoreStoreObject { class Person: CoreStoreObject {
let name = Value.Optional<String>("name")
let pets = Relationship.ToManyUnordered<Animal>("pets", inverse: { $0.master }) @Field.Stored("name")
var name: String?
@Field.Relationship("pets", inverse: \.$master)
var pets: Set<Animal>
} }
/// ======================= /// =======================
@@ -24,6 +34,10 @@ let dataStack = DataStack(
entities: [ entities: [
Entity<Animal>("Animal"), Entity<Animal>("Animal"),
Entity<Person>("Person") Entity<Person>("Person")
],
versionLock: [
"Animal": [0x4a201cc685d53c0a, 0x16e6c3b561577875, 0xb032e2da61c792a0, 0xa133b801051acee4],
"Person": [0xca938eea1af4bd56, 0xbca30994506356ad, 0x7a7cc655898816ef, 0x1a4551ffedc9b214]
] ]
) )
) )
@@ -42,12 +56,12 @@ dataStack.addStorage(
asynchronous: { transaction in asynchronous: { transaction in
let animal = transaction.create(Into<Animal>()) let animal = transaction.create(Into<Animal>())
animal.species .= "Sparrow" animal.species = "Sparrow"
animal.color .= .yellow animal.color = .yellow
let person = transaction.create(Into<Person>()) let person = transaction.create(Into<Person>())
person.name .= "John" person.name = "John"
person.pets.value.insert(animal) person.pets.insert(animal)
}, },
completion: { result in completion: { result in
@@ -58,13 +72,16 @@ dataStack.addStorage(
case .success: case .success:
/// Accessing Objects ===== /// Accessing Objects =====
let bird = try! dataStack.fetchOne(From<Animal>().where(\.species == "Sparrow"))! let bird = try! dataStack.fetchOne(
print(bird.species.value) From<Animal>()
print(bird.color.value as Any) .where(\.$species == "Sparrow")
)!
print(bird.species)
print(bird.color as Any)
print(bird) print(bird)
let owner = bird.master.value! let owner = bird.master!
print(owner.name.value as Any) print(owner.name as Any)
print(owner.pets.count) print(owner.pets.count)
print(owner) print(owner)
/// ======================= /// =======================

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'> <playground version='5.0' target-platform='ios' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/> <timeline fileName='timeline.xctimeline'/>
</playground> </playground>

View File

@@ -6,14 +6,24 @@ PlaygroundPage.current.needsIndefiniteExecution = true
/// Model Declaration ===== /// Model Declaration =====
class Animal: CoreStoreObject { class Animal: CoreStoreObject {
let species = Value.Required<String>("species", initial: "Swift")
let master = Relationship.ToOne<Person>("master") @Field.Stored("species")
let color = Transformable.Optional<NSColor>("color", initial: .orange) var species: String = "Swift"
@Field.Coded("color", coder: FieldCoders.NSCoding.self)
var color: NSColor = .orange
@Field.Relationship("master")
var master: Person?
} }
class Person: CoreStoreObject { class Person: CoreStoreObject {
let name = Value.Optional<String>("name")
let pets = Relationship.ToManyUnordered<Animal>("pets", inverse: { $0.master }) @Field.Stored("name")
var name: String?
@Field.Relationship("pets", inverse: \.$master)
var pets: Set<Animal>
} }
/// ======================= /// =======================
@@ -24,6 +34,10 @@ let dataStack = DataStack(
entities: [ entities: [
Entity<Animal>("Animal"), Entity<Animal>("Animal"),
Entity<Person>("Person") Entity<Person>("Person")
],
versionLock: [
"Animal": [0x4a201cc685d53c0a, 0x16e6c3b561577875, 0xb032e2da61c792a0, 0xa133b801051acee4],
"Person": [0xca938eea1af4bd56, 0xbca30994506356ad, 0x7a7cc655898816ef, 0x1a4551ffedc9b214]
] ]
) )
) )
@@ -42,12 +56,12 @@ dataStack.addStorage(
asynchronous: { transaction in asynchronous: { transaction in
let animal = transaction.create(Into<Animal>()) let animal = transaction.create(Into<Animal>())
animal.species .= "Sparrow" animal.species = "Sparrow"
animal.color .= .yellow animal.color = .yellow
let person = transaction.create(Into<Person>()) let person = transaction.create(Into<Person>())
person.name .= "John" person.name = "John"
person.pets.value.insert(animal) person.pets.insert(animal)
}, },
completion: { result in completion: { result in
@@ -58,13 +72,16 @@ dataStack.addStorage(
case .success: case .success:
/// Accessing Objects ===== /// Accessing Objects =====
let bird = try! dataStack.fetchOne(From<Animal>().where(\.species == "Sparrow"))! let bird = try! dataStack.fetchOne(
print(bird.species.value) From<Animal>()
print(bird.color.value as Any) .where(\.$species == "Sparrow")
)!
print(bird.species)
print(bird.color as Any)
print(bird) print(bird)
let owner = bird.master.value! let owner = bird.master!
print(owner.name.value as Any) print(owner.name as Any)
print(owner.pets.count) print(owner.pets.count)
print(owner) print(owner)
/// ======================= /// =======================

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'> <playground version='5.0' target-platform='macos' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/> <timeline fileName='timeline.xctimeline'/>
</playground> </playground>