From 95fe4ad9296fa815120f954e971749d599235201 Mon Sep 17 00:00:00 2001 From: Yue Cai Date: Sun, 5 Jul 2020 23:30:26 +0800 Subject: [PATCH] optimize the demo code --- .../project.pbxproj | 8 ++--- Example/IceCream_Example/AppDelegate.swift | 4 +-- .../IceCream_Example/CatsViewController.swift | 16 ++++----- .../IceCream_Example/DogsViewController.swift | 18 +++++----- ...oller.swift => OwnersViewController.swift} | 34 +++++++++---------- Example/IceCream_Example/Person.swift | 2 +- .../TabBarViewController.swift | 4 +-- 7 files changed, 43 insertions(+), 43 deletions(-) rename Example/IceCream_Example/{DevelopersViewController.swift => OwnersViewController.swift} (77%) diff --git a/Example/IceCream_Example.xcodeproj/project.pbxproj b/Example/IceCream_Example.xcodeproj/project.pbxproj index b2a45d6a..19b88ba0 100644 --- a/Example/IceCream_Example.xcodeproj/project.pbxproj +++ b/Example/IceCream_Example.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 0DFDEF8022622B47002C7CC9 /* DevelopersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DFDEF7F22622B47002C7CC9 /* DevelopersViewController.swift */; }; + 0D4EF51B24B228570036FD90 /* OwnersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D4EF51A24B228570036FD90 /* OwnersViewController.swift */; }; 6743F6EC202B0CB600912163 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6743F6EB202B0CB600912163 /* AppDelegate.swift */; }; 6743F6EE202B0CB600912163 /* DogsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6743F6ED202B0CB600912163 /* DogsViewController.swift */; }; 6743F6FE202B0D1400912163 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6743F6FD202B0D1400912163 /* Images.xcassets */; }; @@ -25,7 +25,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 0DFDEF7F22622B47002C7CC9 /* DevelopersViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevelopersViewController.swift; sourceTree = ""; }; + 0D4EF51A24B228570036FD90 /* OwnersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OwnersViewController.swift; sourceTree = ""; }; 6743F6E8202B0CB600912163 /* IceCream_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IceCream_Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6743F6EB202B0CB600912163 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 6743F6ED202B0CB600912163 /* DogsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DogsViewController.swift; sourceTree = ""; }; @@ -86,7 +86,7 @@ 6743F702202B0D2400912163 /* Dog.swift */, 67AA235E20B84C9700953F09 /* CatsViewController.swift */, 6743F6ED202B0CB600912163 /* DogsViewController.swift */, - 0DFDEF7F22622B47002C7CC9 /* DevelopersViewController.swift */, + 0D4EF51A24B228570036FD90 /* OwnersViewController.swift */, 6743F6FF202B0D1C00912163 /* LaunchScreen.xib */, 6743F6FD202B0D1400912163 /* Images.xcassets */, 6743F6EB202B0CB600912163 /* AppDelegate.swift */, @@ -195,10 +195,10 @@ files = ( 6743F6EE202B0CB600912163 /* DogsViewController.swift in Sources */, 67AA236020B84C9700953F09 /* CatsViewController.swift in Sources */, + 0D4EF51B24B228570036FD90 /* OwnersViewController.swift in Sources */, 67AA236120B84C9700953F09 /* Cat.swift in Sources */, 6743F6EC202B0CB600912163 /* AppDelegate.swift in Sources */, 67851AF920FAFE0F00D5142C /* Person.swift in Sources */, - 0DFDEF8022622B47002C7CC9 /* DevelopersViewController.swift in Sources */, 67AA236320B84CB600953F09 /* TabBarViewController.swift in Sources */, 6743F703202B0D2400912163 /* Dog.swift in Sources */, ); diff --git a/Example/IceCream_Example/AppDelegate.swift b/Example/IceCream_Example/AppDelegate.swift index 63f8266c..0cf702d1 100644 --- a/Example/IceCream_Example/AppDelegate.swift +++ b/Example/IceCream_Example/AppDelegate.swift @@ -19,9 +19,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { syncEngine = SyncEngine(objects: [ - SyncObject(), SyncObject(), - SyncObject() + SyncObject(), + SyncObject(), ]) /// If you wanna test public Database, comment the above syncEngine code and uncomment the following one diff --git a/Example/IceCream_Example/CatsViewController.swift b/Example/IceCream_Example/CatsViewController.swift index 1ffddec9..888ed07d 100644 --- a/Example/IceCream_Example/CatsViewController.swift +++ b/Example/IceCream_Example/CatsViewController.swift @@ -12,19 +12,19 @@ import IceCream import RxRealm import RxSwift -class CatsViewController: UIViewController { +final class CatsViewController: UIViewController { - var cats: [Cat] = [] - let bag = DisposeBag() + private var cats: [Cat] = [] + private let bag = DisposeBag() - let realm = try! Realm() + private let realm = try! Realm() - lazy var addBarItem: UIBarButtonItem = { + private lazy var addBarItem: UIBarButtonItem = { let b = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(add)) return b }() - lazy var tableView: UITableView = { + private lazy var tableView: UITableView = { let tv = UITableView() tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell") tv.delegate = self @@ -47,7 +47,7 @@ class CatsViewController: UIViewController { tableView.frame = view.frame } - func bind() { + private func bind() { let realm = try! Realm() /// Results instances are live, auto-updating views into the underlying data, which means results never have to be re-fetched. @@ -62,7 +62,7 @@ class CatsViewController: UIViewController { }).disposed(by: bag) } - @objc func add() { + @objc private func add() { let cat = Cat() cat.name = "Cat Number " + "\(cats.count)" cat.age = cats.count + 1 diff --git a/Example/IceCream_Example/DogsViewController.swift b/Example/IceCream_Example/DogsViewController.swift index 6dc72eb7..d676f734 100644 --- a/Example/IceCream_Example/DogsViewController.swift +++ b/Example/IceCream_Example/DogsViewController.swift @@ -12,20 +12,20 @@ import IceCream import RxRealm import RxSwift -class DogsViewController: UIViewController { +final class DogsViewController: UIViewController { - let jim = Person() - var dogs: [Dog] = [] - let bag = DisposeBag() + private let jim = Person() + private var dogs: [Dog] = [] + private let bag = DisposeBag() - let realm = try! Realm() + private let realm = try! Realm() - lazy var addBarItem: UIBarButtonItem = { + private lazy var addBarItem: UIBarButtonItem = { let b = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(add)) return b }() - lazy var tableView: UITableView = { + private lazy var tableView: UITableView = { let tv = UITableView() tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell") tv.delegate = self @@ -48,7 +48,7 @@ class DogsViewController: UIViewController { tableView.frame = view.frame } - func bind() { + private func bind() { let realm = try! Realm() /// Results instances are live, auto-updating views into the underlying data, which means results never have to be re-fetched. @@ -63,7 +63,7 @@ class DogsViewController: UIViewController { }).disposed(by: bag) } - @objc func add() { + @objc private func add() { let dog = Dog() dog.name = "Dog Number " + "\(dogs.count)" dog.age = dogs.count + 1 diff --git a/Example/IceCream_Example/DevelopersViewController.swift b/Example/IceCream_Example/OwnersViewController.swift similarity index 77% rename from Example/IceCream_Example/DevelopersViewController.swift rename to Example/IceCream_Example/OwnersViewController.swift index 852f827b..2983aa19 100644 --- a/Example/IceCream_Example/DevelopersViewController.swift +++ b/Example/IceCream_Example/OwnersViewController.swift @@ -11,19 +11,19 @@ import RealmSwift import RxRealm import RxSwift -class DevelopersViewController: UIViewController { +final class OwnersViewController: UIViewController { - var developers: [Person] = [] + var owners: [Person] = [] let bag = DisposeBag() let realm = try! Realm() - lazy var addBarItem: UIBarButtonItem = { + private lazy var addBarItem: UIBarButtonItem = { let b = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(add)) return b }() - lazy var tableView: UITableView = { + private lazy var tableView: UITableView = { let tv = UITableView() tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell") tv.delegate = self @@ -36,7 +36,7 @@ class DevelopersViewController: UIViewController { view.addSubview(tableView) navigationItem.rightBarButtonItem = addBarItem - title = "Developers" + title = "Owners" bind() } @@ -46,22 +46,22 @@ class DevelopersViewController: UIViewController { tableView.frame = view.frame } - func bind() { + private func bind() { let realm = try! Realm() /// Results instances are live, auto-updating views into the underlying data, which means results never have to be re-fetched. /// https://realm.io/docs/swift/latest/#objects-with-primary-keys - let developers = realm.objects(Person.self) + let owners = realm.objects(Person.self) - Observable.array(from: developers).subscribe(onNext: { (developers) in + Observable.array(from: owners).subscribe(onNext: { (owners) in /// When developers data changes in Realm, the following code will be executed /// It works like magic. - self.developers = developers.filter { !$0.isDeleted } + self.owners = owners.filter { !$0.isDeleted } self.tableView.reloadData() }).disposed(by: bag) } - @objc func add() { + @objc private func add() { let user = Person() user.name = "Yue Cai" @@ -71,15 +71,15 @@ class DevelopersViewController: UIViewController { } } -extension DevelopersViewController: UITableViewDelegate { +extension OwnersViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (_, ip) in let alert = UIAlertController(title: NSLocalizedString("caution", comment: "caution"), message: NSLocalizedString("sure_to_delete", comment: "sure_to_delete"), preferredStyle: .alert) let deleteAction = UIAlertAction(title: NSLocalizedString("delete", comment: "delete"), style: .destructive, handler: { (action) in - guard ip.row < self.developers.count else { return } - let developer = self.developers[ip.row] + guard ip.row < self.owners.count else { return } + let owner = self.owners[ip.row] try! self.realm.write { - developer.isDeleted = true + owner.isDeleted = true } }) let defaultAction = UIAlertAction(title: NSLocalizedString("cancel", comment: "cancel"), style: .default, handler: nil) @@ -91,14 +91,14 @@ extension DevelopersViewController: UITableViewDelegate { } } -extension DevelopersViewController: UITableViewDataSource { +extension OwnersViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return developers.count + return owners.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") - cell?.textLabel?.text = developers[indexPath.row].name + cell?.textLabel?.text = owners[indexPath.row].name return cell ?? UITableViewCell() } } diff --git a/Example/IceCream_Example/Person.swift b/Example/IceCream_Example/Person.swift index 0e36adc7..63059a66 100644 --- a/Example/IceCream_Example/Person.swift +++ b/Example/IceCream_Example/Person.swift @@ -15,7 +15,7 @@ class Person: Object { @objc dynamic var name = "Jim" @objc dynamic var isDeleted = false - let dogs = LinkingObjects(fromType: Dog.self, property: "owner") + var cats = List() override class func primaryKey() -> String? { return "id" diff --git a/Example/IceCream_Example/TabBarViewController.swift b/Example/IceCream_Example/TabBarViewController.swift index 37e52e13..adacede3 100644 --- a/Example/IceCream_Example/TabBarViewController.swift +++ b/Example/IceCream_Example/TabBarViewController.swift @@ -8,14 +8,14 @@ import UIKit -class TabBarViewController: UITabBarController { +final class TabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() viewControllers = [ UINavigationController(rootViewController: DogsViewController()), UINavigationController(rootViewController: CatsViewController()), - UINavigationController(rootViewController: DevelopersViewController()) + UINavigationController(rootViewController: OwnersViewController()) ] }