mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
GRDBDemoiOS: simplify view controllers’ names
This commit is contained in:
1 parent
669720c6dc
commit
894bb05cba
5 files changed
+268
-268
No files matched your search
@@ -8,7 +8,7 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
568C7A9A1CA6C63100796484 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7A991CA6C63100796484 /* AppDelegate.swift */; };
|
||||
568C7A9C1CA6C63100796484 /* FetchedRecordsControllerDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7A9B1CA6C63100796484 /* FetchedRecordsControllerDemoViewController.swift */; };
|
||||
568C7A9C1CA6C63100796484 /* PersonsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7A9B1CA6C63100796484 /* PersonsViewController.swift */; };
|
||||
568C7A9F1CA6C63100796484 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 568C7A9D1CA6C63100796484 /* Main.storyboard */; };
|
||||
568C7AA11CA6C63100796484 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 568C7AA01CA6C63100796484 /* Assets.xcassets */; };
|
||||
568C7AA41CA6C63100796484 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 568C7AA21CA6C63100796484 /* LaunchScreen.storyboard */; };
|
||||
@@ -18,7 +18,7 @@
|
||||
568C7AE21CA6C97500796484 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7AE11CA6C97500796484 /* Database.swift */; };
|
||||
568C7AE41CA6CEBE00796484 /* PersonEditionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7AE31CA6CEBE00796484 /* PersonEditionViewController.swift */; };
|
||||
568C7AE61CA6F95300796484 /* DemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7AE51CA6F95300796484 /* DemoViewController.swift */; };
|
||||
568C7AE81CA6FB6600796484 /* PersonsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7AE71CA6FB6600796484 /* PersonsViewController.swift */; };
|
||||
568C7AE81CA6FB6600796484 /* SimplePersonsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568C7AE71CA6FB6600796484 /* SimplePersonsViewController.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
@@ -90,7 +90,7 @@
|
||||
/* Begin PBXFileReference section */
|
||||
568C7A961CA6C63100796484 /* GRDBDemoiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GRDBDemoiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
568C7A991CA6C63100796484 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
568C7A9B1CA6C63100796484 /* FetchedRecordsControllerDemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchedRecordsControllerDemoViewController.swift; sourceTree = "<group>"; };
|
||||
568C7A9B1CA6C63100796484 /* PersonsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonsViewController.swift; sourceTree = "<group>"; };
|
||||
568C7A9E1CA6C63100796484 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
|
||||
568C7AA01CA6C63100796484 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
568C7AA31CA6C63100796484 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
|
||||
@@ -100,7 +100,7 @@
|
||||
568C7AE11CA6C97500796484 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = "<group>"; };
|
||||
568C7AE31CA6CEBE00796484 /* PersonEditionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonEditionViewController.swift; sourceTree = "<group>"; };
|
||||
568C7AE51CA6F95300796484 /* DemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoViewController.swift; sourceTree = "<group>"; };
|
||||
568C7AE71CA6FB6600796484 /* PersonsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonsViewController.swift; sourceTree = "<group>"; };
|
||||
568C7AE71CA6FB6600796484 /* SimplePersonsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimplePersonsViewController.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -164,9 +164,9 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
568C7AE51CA6F95300796484 /* DemoViewController.swift */,
|
||||
568C7A9B1CA6C63100796484 /* FetchedRecordsControllerDemoViewController.swift */,
|
||||
568C7AE31CA6CEBE00796484 /* PersonEditionViewController.swift */,
|
||||
568C7AE71CA6FB6600796484 /* PersonsViewController.swift */,
|
||||
568C7A9B1CA6C63100796484 /* PersonsViewController.swift */,
|
||||
568C7AE71CA6FB6600796484 /* SimplePersonsViewController.swift */,
|
||||
);
|
||||
name = Controllers;
|
||||
sourceTree = "<group>";
|
||||
@@ -297,8 +297,8 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
568C7AE01CA6C7BC00796484 /* Person.swift in Sources */,
|
||||
568C7AE81CA6FB6600796484 /* PersonsViewController.swift in Sources */,
|
||||
568C7A9C1CA6C63100796484 /* FetchedRecordsControllerDemoViewController.swift in Sources */,
|
||||
568C7AE81CA6FB6600796484 /* SimplePersonsViewController.swift in Sources */,
|
||||
568C7A9C1CA6C63100796484 /* PersonsViewController.swift in Sources */,
|
||||
568C7AE21CA6C97500796484 /* Database.swift in Sources */,
|
||||
568C7AE61CA6F95300796484 /* DemoViewController.swift in Sources */,
|
||||
568C7AE41CA6CEBE00796484 /* PersonEditionViewController.swift in Sources */,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<!--Hall Of Fame-->
|
||||
<scene sceneID="8jG-C9-QsI">
|
||||
<objects>
|
||||
<tableViewController id="cyv-nk-Plb" customClass="PersonsViewController" customModule="GRDBDemoiOS" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableViewController id="cyv-nk-Plb" customClass="SimplePersonsViewController" customModule="GRDBDemoiOS" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="oON-zg-8kO">
|
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
@@ -249,7 +249,7 @@
|
||||
<!--Hall Of Fame-->
|
||||
<scene sceneID="9su-ms-vdJ">
|
||||
<objects>
|
||||
<tableViewController id="PJp-Eq-ZjA" customClass="FetchedRecordsControllerDemoViewController" customModule="GRDBDemoiOS" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableViewController id="PJp-Eq-ZjA" customClass="PersonsViewController" customModule="GRDBDemoiOS" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="tX0-ON-WSI">
|
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
|
||||
@@ -1,237 +0,0 @@
|
||||
import UIKit
|
||||
import GRDB
|
||||
|
||||
class FetchedRecordsControllerDemoViewController: UITableViewController {
|
||||
var personsController: FetchedRecordsController<Person>!
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: .addPerson),
|
||||
editButtonItem()
|
||||
]
|
||||
|
||||
let request = personsSortedByScore
|
||||
personsController = FetchedRecordsController(dbQueue, request: request, compareRecordsByPrimaryKey: true)
|
||||
personsController.delegate = self
|
||||
personsController.performFetch()
|
||||
|
||||
configureToolbar()
|
||||
}
|
||||
|
||||
override func viewWillAppear(animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
navigationController?.toolbarHidden = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Navigation
|
||||
|
||||
extension FetchedRecordsControllerDemoViewController : PersonEditionViewControllerDelegate {
|
||||
|
||||
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
||||
if segue.identifier == "EditPerson" {
|
||||
let person = personsController.recordAtIndexPath(tableView.indexPathForSelectedRow!)
|
||||
let controller = segue.destinationViewController as! PersonEditionViewController
|
||||
controller.title = person.name
|
||||
controller.person = person
|
||||
controller.delegate = self // we will save person when back button is tapped
|
||||
controller.cancelButtonHidden = true
|
||||
controller.commitButtonHidden = true
|
||||
}
|
||||
else if segue.identifier == "NewPerson" {
|
||||
setEditing(false, animated: true)
|
||||
let navigationController = segue.destinationViewController as! UINavigationController
|
||||
let controller = navigationController.viewControllers.first as! PersonEditionViewController
|
||||
controller.title = "New Person"
|
||||
controller.person = Person(name: "", score: 0)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func addPerson(sender: AnyObject?) {
|
||||
performSegueWithIdentifier("NewPerson", sender: sender)
|
||||
}
|
||||
|
||||
@IBAction func cancelPersonEdition(segue: UIStoryboardSegue) {
|
||||
// Person creation: cancel button was tapped
|
||||
}
|
||||
|
||||
@IBAction func commitPersonEdition(segue: UIStoryboardSegue) {
|
||||
// Person creation: commit button was tapped
|
||||
let controller = segue.sourceViewController as! PersonEditionViewController
|
||||
controller.applyChanges()
|
||||
if !controller.person.name.isEmpty {
|
||||
try! controller.person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func personEditionControllerDidComplete(controller: PersonEditionViewController) {
|
||||
// Person edition: back button was tapped
|
||||
controller.applyChanges()
|
||||
if !controller.person.name.isEmpty {
|
||||
try! controller.person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UITableViewDataSource
|
||||
|
||||
extension FetchedRecordsControllerDemoViewController {
|
||||
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
|
||||
let person = personsController.recordAtIndexPath(indexPath)
|
||||
cell.textLabel?.text = person.name
|
||||
cell.detailTextLabel?.text = abs(person.score) > 1 ? "\(person.score) points" : "0 point"
|
||||
}
|
||||
|
||||
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
||||
return personsController.sections.count
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return personsController.sections[section].numberOfRecords
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCellWithIdentifier("Person", forIndexPath: indexPath)
|
||||
configureCell(cell, atIndexPath: indexPath)
|
||||
return cell
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
|
||||
// Delete the person
|
||||
let person = personsController.recordAtIndexPath(indexPath)
|
||||
try! person.delete(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - FetchedRecordsControllerDelegate
|
||||
|
||||
extension FetchedRecordsControllerDemoViewController : FetchedRecordsControllerDelegate {
|
||||
|
||||
func controllerWillChangeRecords<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.beginUpdates()
|
||||
}
|
||||
|
||||
func controller<T>(controller: FetchedRecordsController<T>, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) {
|
||||
switch event {
|
||||
case .Insertion(let indexPath):
|
||||
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
|
||||
case .Deletion(let indexPath):
|
||||
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
|
||||
case .Update(let indexPath, _):
|
||||
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
|
||||
configureCell(cell, atIndexPath: indexPath)
|
||||
}
|
||||
|
||||
case .Move(let indexPath, let newIndexPath, _):
|
||||
// Actually move cells around for more demo effect :-)
|
||||
let cell = tableView.cellForRowAtIndexPath(indexPath)
|
||||
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
|
||||
if let cell = cell {
|
||||
configureCell(cell, atIndexPath: newIndexPath)
|
||||
}
|
||||
|
||||
// A quieter animation:
|
||||
// tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
// tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
|
||||
}
|
||||
}
|
||||
|
||||
func controllerDidChangeRecords<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.endUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - FetchedRecordsController Demo
|
||||
|
||||
extension FetchedRecordsControllerDemoViewController {
|
||||
|
||||
private func configureToolbar() {
|
||||
toolbarItems = [
|
||||
UIBarButtonItem(title: "Name ⬆︎", style: .Plain, target: self, action: .sortByName),
|
||||
UIBarButtonItem(title: "Score ⬇︎", style: .Plain, target: self, action: .sortByScore),
|
||||
UIBarButtonItem(title: "Randomize", style: .Plain, target: self, action: .randomizeScores),
|
||||
UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil),
|
||||
UIBarButtonItem(title: "💣", style: .Plain, target: self, action: .stressTest)
|
||||
]
|
||||
}
|
||||
|
||||
@IBAction func sortByName() {
|
||||
setEditing(false, animated: true)
|
||||
personsController.setRequest(personsSortedByName)
|
||||
}
|
||||
|
||||
@IBAction func sortByScore() {
|
||||
setEditing(false, animated: true)
|
||||
personsController.setRequest(personsSortedByScore)
|
||||
}
|
||||
|
||||
@IBAction func randomizeScores() {
|
||||
setEditing(false, animated: true)
|
||||
|
||||
try! dbQueue.inTransaction { db in
|
||||
for person in Person.fetch(db) {
|
||||
person.score = randomScore()
|
||||
try person.update(db)
|
||||
}
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func stressTest() {
|
||||
setEditing(false, animated: true)
|
||||
|
||||
// Spawn some concurrent background jobs
|
||||
for _ in 0..<20 {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
|
||||
try! dbQueue.inTransaction { db in
|
||||
if Person.fetchCount(db) == 0 {
|
||||
// Insert persons
|
||||
for _ in 0..<8 {
|
||||
try Person(name: randomName(), score: randomScore()).insert(db)
|
||||
}
|
||||
} else {
|
||||
// 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(2) == 0 {
|
||||
person.score = randomScore()
|
||||
try person.update(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3
|
||||
private extension Selector {
|
||||
static let addPerson = #selector(FetchedRecordsControllerDemoViewController.addPerson(_:))
|
||||
static let sortByName = #selector(FetchedRecordsControllerDemoViewController.sortByName)
|
||||
static let sortByScore = #selector(FetchedRecordsControllerDemoViewController.sortByScore)
|
||||
static let randomizeScores = #selector(FetchedRecordsControllerDemoViewController.randomizeScores)
|
||||
static let stressTest = #selector(FetchedRecordsControllerDemoViewController.stressTest)
|
||||
}
|
||||
|
||||
private let personsSortedByName = Person.order(SQLColumn("name"))
|
||||
private let personsSortedByScore = Person.order(SQLColumn("score").desc, SQLColumn("name"))
|
||||
@@ -2,25 +2,27 @@ import UIKit
|
||||
import GRDB
|
||||
|
||||
class PersonsViewController: UITableViewController {
|
||||
var persons: [Person]!
|
||||
|
||||
var personsController: FetchedRecordsController<Person>!
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(PersonsViewController.addPerson(_:))),
|
||||
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: .addPerson),
|
||||
editButtonItem()
|
||||
]
|
||||
|
||||
let request = personsSortedByScore
|
||||
personsController = FetchedRecordsController(dbQueue, request: request, compareRecordsByPrimaryKey: true)
|
||||
personsController.delegate = self
|
||||
personsController.performFetch()
|
||||
|
||||
configureToolbar()
|
||||
}
|
||||
|
||||
override func viewWillAppear(animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
loadPersons()
|
||||
tableView.reloadData()
|
||||
}
|
||||
|
||||
private func loadPersons() {
|
||||
persons = Person.order(SQLColumn("score").desc, SQLColumn("name")).fetchAll(dbQueue)
|
||||
navigationController?.toolbarHidden = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +33,7 @@ extension PersonsViewController : PersonEditionViewControllerDelegate {
|
||||
|
||||
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
||||
if segue.identifier == "EditPerson" {
|
||||
let person = persons[tableView.indexPathForSelectedRow!.row]
|
||||
let person = personsController.recordAtIndexPath(tableView.indexPathForSelectedRow!)
|
||||
let controller = segue.destinationViewController as! PersonEditionViewController
|
||||
controller.title = person.name
|
||||
controller.person = person
|
||||
@@ -60,18 +62,16 @@ extension PersonsViewController : PersonEditionViewControllerDelegate {
|
||||
// Person creation: commit button was tapped
|
||||
let controller = segue.sourceViewController as! PersonEditionViewController
|
||||
controller.applyChanges()
|
||||
let person = controller.person
|
||||
if !person.name.isEmpty {
|
||||
try! person.save(dbQueue)
|
||||
if !controller.person.name.isEmpty {
|
||||
try! controller.person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func personEditionControllerDidComplete(controller: PersonEditionViewController) {
|
||||
// Person edition: back button was tapped
|
||||
controller.applyChanges()
|
||||
let person = controller.person
|
||||
if !person.name.isEmpty {
|
||||
try! person.save(dbQueue)
|
||||
if !controller.person.name.isEmpty {
|
||||
try! controller.person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,13 +81,17 @@ extension PersonsViewController : PersonEditionViewControllerDelegate {
|
||||
|
||||
extension PersonsViewController {
|
||||
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
|
||||
let person = persons[indexPath.row]
|
||||
let person = personsController.recordAtIndexPath(indexPath)
|
||||
cell.textLabel?.text = person.name
|
||||
cell.detailTextLabel?.text = abs(person.score) > 1 ? "\(person.score) points" : "0 point"
|
||||
}
|
||||
|
||||
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
||||
return personsController.sections.count
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return persons.count
|
||||
return personsController.sections[section].numberOfRecords
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
||||
@@ -98,9 +102,136 @@ extension PersonsViewController {
|
||||
|
||||
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
|
||||
// Delete the person
|
||||
let person = persons[indexPath.row]
|
||||
let person = personsController.recordAtIndexPath(indexPath)
|
||||
try! person.delete(dbQueue)
|
||||
persons.removeAtIndex(indexPath.row)
|
||||
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - FetchedRecordsControllerDelegate
|
||||
|
||||
extension PersonsViewController : FetchedRecordsControllerDelegate {
|
||||
|
||||
func controllerWillChangeRecords<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.beginUpdates()
|
||||
}
|
||||
|
||||
func controller<T>(controller: FetchedRecordsController<T>, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) {
|
||||
switch event {
|
||||
case .Insertion(let indexPath):
|
||||
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
|
||||
case .Deletion(let indexPath):
|
||||
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
|
||||
case .Update(let indexPath, _):
|
||||
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
|
||||
configureCell(cell, atIndexPath: indexPath)
|
||||
}
|
||||
|
||||
case .Move(let indexPath, let newIndexPath, _):
|
||||
// Actually move cells around for more demo effect :-)
|
||||
let cell = tableView.cellForRowAtIndexPath(indexPath)
|
||||
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
|
||||
if let cell = cell {
|
||||
configureCell(cell, atIndexPath: newIndexPath)
|
||||
}
|
||||
|
||||
// A quieter animation:
|
||||
// tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
// tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
|
||||
}
|
||||
}
|
||||
|
||||
func controllerDidChangeRecords<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.endUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - FetchedRecordsController Demo
|
||||
|
||||
extension PersonsViewController {
|
||||
|
||||
private func configureToolbar() {
|
||||
toolbarItems = [
|
||||
UIBarButtonItem(title: "Name ⬆︎", style: .Plain, target: self, action: .sortByName),
|
||||
UIBarButtonItem(title: "Score ⬇︎", style: .Plain, target: self, action: .sortByScore),
|
||||
UIBarButtonItem(title: "Randomize", style: .Plain, target: self, action: .randomizeScores),
|
||||
UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil),
|
||||
UIBarButtonItem(title: "💣", style: .Plain, target: self, action: .stressTest)
|
||||
]
|
||||
}
|
||||
|
||||
@IBAction func sortByName() {
|
||||
setEditing(false, animated: true)
|
||||
personsController.setRequest(personsSortedByName)
|
||||
}
|
||||
|
||||
@IBAction func sortByScore() {
|
||||
setEditing(false, animated: true)
|
||||
personsController.setRequest(personsSortedByScore)
|
||||
}
|
||||
|
||||
@IBAction func randomizeScores() {
|
||||
setEditing(false, animated: true)
|
||||
|
||||
try! dbQueue.inTransaction { db in
|
||||
for person in Person.fetch(db) {
|
||||
person.score = randomScore()
|
||||
try person.update(db)
|
||||
}
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func stressTest() {
|
||||
setEditing(false, animated: true)
|
||||
|
||||
// Spawn some concurrent background jobs
|
||||
for _ in 0..<20 {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
|
||||
try! dbQueue.inTransaction { db in
|
||||
if Person.fetchCount(db) == 0 {
|
||||
// Insert persons
|
||||
for _ in 0..<8 {
|
||||
try Person(name: randomName(), score: randomScore()).insert(db)
|
||||
}
|
||||
} else {
|
||||
// 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(2) == 0 {
|
||||
person.score = randomScore()
|
||||
try person.update(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3
|
||||
private extension Selector {
|
||||
static let addPerson = #selector(PersonsViewController.addPerson(_:))
|
||||
static let sortByName = #selector(PersonsViewController.sortByName)
|
||||
static let sortByScore = #selector(PersonsViewController.sortByScore)
|
||||
static let randomizeScores = #selector(PersonsViewController.randomizeScores)
|
||||
static let stressTest = #selector(PersonsViewController.stressTest)
|
||||
}
|
||||
|
||||
private let personsSortedByName = Person.order(SQLColumn("name"))
|
||||
private let personsSortedByScore = Person.order(SQLColumn("score").desc, SQLColumn("name"))
|
||||
@@ -0,0 +1,106 @@
|
||||
import UIKit
|
||||
import GRDB
|
||||
|
||||
class SimplePersonsViewController: UITableViewController {
|
||||
var persons: [Person]!
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(SimplePersonsViewController.addPerson(_:))),
|
||||
editButtonItem()
|
||||
]
|
||||
}
|
||||
|
||||
override func viewWillAppear(animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
loadPersons()
|
||||
tableView.reloadData()
|
||||
}
|
||||
|
||||
private func loadPersons() {
|
||||
persons = Person.order(SQLColumn("score").desc, SQLColumn("name")).fetchAll(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Navigation
|
||||
|
||||
extension SimplePersonsViewController : PersonEditionViewControllerDelegate {
|
||||
|
||||
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
||||
if segue.identifier == "EditPerson" {
|
||||
let person = persons[tableView.indexPathForSelectedRow!.row]
|
||||
let controller = segue.destinationViewController as! PersonEditionViewController
|
||||
controller.title = person.name
|
||||
controller.person = person
|
||||
controller.delegate = self // we will save person when back button is tapped
|
||||
controller.cancelButtonHidden = true
|
||||
controller.commitButtonHidden = true
|
||||
}
|
||||
else if segue.identifier == "NewPerson" {
|
||||
setEditing(false, animated: true)
|
||||
let navigationController = segue.destinationViewController as! UINavigationController
|
||||
let controller = navigationController.viewControllers.first as! PersonEditionViewController
|
||||
controller.title = "New Person"
|
||||
controller.person = Person(name: "", score: 0)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func addPerson(sender: AnyObject?) {
|
||||
performSegueWithIdentifier("NewPerson", sender: sender)
|
||||
}
|
||||
|
||||
@IBAction func cancelPersonEdition(segue: UIStoryboardSegue) {
|
||||
// Person creation: cancel button was tapped
|
||||
}
|
||||
|
||||
@IBAction func commitPersonEdition(segue: UIStoryboardSegue) {
|
||||
// Person creation: commit button was tapped
|
||||
let controller = segue.sourceViewController as! PersonEditionViewController
|
||||
controller.applyChanges()
|
||||
let person = controller.person
|
||||
if !person.name.isEmpty {
|
||||
try! person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func personEditionControllerDidComplete(controller: PersonEditionViewController) {
|
||||
// Person edition: back button was tapped
|
||||
controller.applyChanges()
|
||||
let person = controller.person
|
||||
if !person.name.isEmpty {
|
||||
try! person.save(dbQueue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UITableViewDataSource
|
||||
|
||||
extension SimplePersonsViewController {
|
||||
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
|
||||
let person = persons[indexPath.row]
|
||||
cell.textLabel?.text = person.name
|
||||
cell.detailTextLabel?.text = abs(person.score) > 1 ? "\(person.score) points" : "0 point"
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return persons.count
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCellWithIdentifier("Person", forIndexPath: indexPath)
|
||||
configureCell(cell, atIndexPath: indexPath)
|
||||
return cell
|
||||
}
|
||||
|
||||
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
|
||||
// Delete the person
|
||||
let person = persons[indexPath.row]
|
||||
try! person.delete(dbQueue)
|
||||
persons.removeAtIndex(indexPath.row)
|
||||
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user