GRDBDemoiOS WIP

This commit is contained in:
Gwendal Roué committed 2016-03-27 01:44:43 +01:00
1 parent fb601ee80f
commit 8af5f54767
2 files changed
+52 -10

No files matched your search

@@ -40,12 +40,20 @@ func setupDatabase() {
")")
}
migrator.registerMigration("addPersons") { db in
try Person(name: "Alice", score: 150).insert(db)
try Person(name: "Bob", score: 70).insert(db)
try Person(name: "Craig", score: 220).insert(db)
try Person(name: "David", score: 80).insert(db)
try Person(name: "Elise", score: 100).insert(db)
try Person(name: "Fiona", score: 40).insert(db)
for _ in 0..<8 {
try Person(name: randomName(), score: randomScore()).insert(db)
}
}
try! migrator.migrate(dbQueue)
}
}
private let names = ["Arthur", "Anita", "Barbara", "Bernard", "Craig", "Chiara", "David", "Dean", "Éric", "Elena", "Fatima", "Frederik", "Gilbert", "Georgette", "Henriette", "Hassan", "Ignacio", "Irene", "Julie", "Jack", "Karl", "Kristel", "Louis", "Liz", "Masashi", "Mary", "Noam", "Nicole", "Ophelie", "Oleg", "Pascal", "Patricia", "Quentin", "Quinn", "Raoul", "Rachel", "Stephan", "Susie", "Tristan", "Tatiana", "Ursule", "Urbain", "Victor", "Violette", "Wilfried", "Wilhelmina", "Yvon", "Yann", "Zazie", "Zoé"]
func randomName() -> String {
return names[Int(arc4random_uniform(UInt32(names.count)))]
}
func randomScore() -> Int {
return 10 * (1 + Int(arc4random_uniform(50)))
}
@@ -167,15 +167,20 @@ extension FetchedRecordsControllerDemoViewController {
style: .Plain,
target: self,
action: #selector(FetchedRecordsControllerDemoViewController.sortByScore)),
UIBarButtonItem(
title: NSLocalizedString("Randomize", comment: ""),
style: .Plain,
target: self,
action: #selector(FetchedRecordsControllerDemoViewController.randomizeScores)),
UIBarButtonItem(
barButtonSystemItem: .FlexibleSpace,
target: nil,
action: nil),
UIBarButtonItem(
title: NSLocalizedString("Randomize Scores", comment: ""),
title: NSLocalizedString("💣", comment: ""),
style: .Plain,
target: self,
action: #selector(FetchedRecordsControllerDemoViewController.randomizeScores))
action: #selector(FetchedRecordsControllerDemoViewController.stress))
]
}
@@ -190,10 +195,39 @@ extension FetchedRecordsControllerDemoViewController {
@IBAction func randomizeScores() {
try! dbQueue.inTransaction { db in
for person in Person.fetch(db) {
person.score = 10 * (1 + Int(arc4random()) % 50)
person.score = randomScore()
try person.update(db)
}
return .Commit
}
}
@IBAction func stress() {
// Spawn some concurrent background jobs
for _ in 0...Int(arc4random_uniform(20)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
try! dbQueue.inTransaction { db in
// Insert a person
if arc4random_uniform(2) == 0 {
let person = Person(name: randomName(), score: randomScore())
try person.insert(db)
}
// Delete a person
if arc4random_uniform(2) == 0 {
if let person = Person.order(sql: "RANDOM()").fetchOne(db) {
try person.delete(db)
}
}
// Update some persons
for person in Person.fetchAll(db) {
if arc4random_uniform(4) == 0 {
person.score = randomScore()
try person.update(db)
}
}
return .Commit
}
}
}
}
}