Skip to content

Commit

Permalink
optimize the demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
caiyue1993 committed Aug 9, 2020
1 parent 73e81a7 commit 95fe4ad
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
8 changes: 4 additions & 4 deletions Example/IceCream_Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -25,7 +25,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0DFDEF7F22622B47002C7CC9 /* DevelopersViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevelopersViewController.swift; sourceTree = "<group>"; };
0D4EF51A24B228570036FD90 /* OwnersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OwnersViewController.swift; sourceTree = "<group>"; };
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 = "<group>"; };
6743F6ED202B0CB600912163 /* DogsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DogsViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
);
Expand Down
4 changes: 2 additions & 2 deletions Example/IceCream_Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

syncEngine = SyncEngine(objects: [
SyncObject<Person>(),
SyncObject<Dog>(),
SyncObject<Cat>()
SyncObject<Cat>(),
SyncObject<Person>(),
])

/// If you wanna test public Database, comment the above syncEngine code and uncomment the following one
Expand Down
16 changes: 8 additions & 8 deletions Example/IceCream_Example/CatsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions Example/IceCream_Example/DogsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +36,7 @@ class DevelopersViewController: UIViewController {

view.addSubview(tableView)
navigationItem.rightBarButtonItem = addBarItem
title = "Developers"
title = "Owners"

bind()
}
Expand All @@ -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"

Expand All @@ -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)
Expand All @@ -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()
}
}
2 changes: 1 addition & 1 deletion Example/IceCream_Example/Person.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cat>()

override class func primaryKey() -> String? {
return "id"
Expand Down
4 changes: 2 additions & 2 deletions Example/IceCream_Example/TabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
]
}

Expand Down

0 comments on commit 95fe4ad

Please sign in to comment.