Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realm meetup Japan #17

Realm meetup Japan #17

- Realm Objective-C/Swift 1.0.2
- RxRealm

Kishikawa Katsumi

July 24, 2016
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. Recent Updates • Realm Objective-C/Swift 1.0.2 (was 1.0.1) • Realm

    Java 1.1.0 (was 1.0.1) • Realm React Native 0.14.0 (was 0.13.0) • Realm Xamarin 0.76.0 [email protected] [email protected]
  2. Realm Objective-C/Swift 1.0.2 • ϓϩύςΟ͕ଘࡏ͠ͳ͍ΫϥεΛอଘ͠Α͏ͱ͢Δͱྫ֎ʹ • write(_:)ϝιουͷϒϩοΫ಺ͰthrowͰ͖ΔΑ͏ʹ • όΠφϦαΠζ͕গ͠ݮগ •

    addNotificationBlock(_:, change:)Ͱॳճͷݺͼग़͠͸ඞ ͣ.Initial͕ݺ͹ΕΔΑ͏ʹ • ٯํ޲ͷؔ࿈͕มߋʹͳΔ৔߹ʹ௨஌͕ݺ͹Εͳ͍໰୊Λमਖ਼ [email protected] [email protected]
  3. Protip for Xcode 8 beta3 and Carthage [email protected] $ echo

    SWIFT_VERSION=\"3.0\">swift3.xcconfig $ XCODE_XCCONFIG_FILE=`pwd`/swift3.xcconfig \ carthage update --platform iOS --no-use-binaries
  4. RxRealm makes Realm observable [email protected] public func asObservable() -> Observable<Self>

    { return Observable.create {observer in let token = self.addNotificationBlock {changeset in let value: Self switch changeset { case .Initial(let latestValue): value = latestValue case .Update(let latestValue, _, _, _): value = latestValue case .Error(let error): observer.onError(error) return } observer.onNext(value) } return AnonymousDisposable { observer.onCompleted() token.stop() } } }
  5. RxRealm makes Realm observable [email protected] public func asObservable() -> Observable<Self>

    { return Observable.create {observer in let token = self.addNotificationBlock {changeset in let value: Self ... observer.onNext(value) } return AnonymousDisposable { observer.onCompleted() token.stop() } } }
  6. RxRealm makes Realm observable [email protected] public func asObservable() -> Observable<Self>

    { return Observable.create {observer in let token = self.addNotificationBlock {changeset in let value: Self ... observer.onNext(value) } return AnonymousDisposable { observer.onCompleted() token.stop() } } }
  7. RxRealm makes Realm observable [email protected] public extension NotificationEmitter where Self:

    RealmCollectionType { public func asObservable() -> Observable<Self> { return Observable.create {...} } }
  8. RxRealm makes Realm observable [email protected] public protocol NotificationEmitter { func

    addNotificationBlock(block: (RealmCollectionChange<Self>) -> ()) -> NotificationToken } extension List: NotificationEmitter {} extension AnyRealmCollection: NotificationEmitter {} extension Results: NotificationEmitter {} extension LinkingObjects: NotificationEmitter {}
  9. RxRealm makes Realm observable [email protected] public extension NotificationEmitter where Self:

    RealmCollectionType { public func asObservable() -> Observable<Self> { return Observable.create {...} } }
  10. Usage [email protected] var token: NotificationToken? func searchWithText(text: String) { token

    = realm .objects(Location) .filter("...") .sorted("...") .addNotificationBlock { (...) in ... tableView.reloadData() } ... }
  11. Example [email protected] /* Observable<Results<Lap>> - wrap Results as observable */

    realm.objects(Lap).asObservable() .map {laps in "\(laps.count) laps"} .subscribeNext {[unowned self]text in self.title = text } .addDisposableTo(bag) /* Observable<Array<Lap>> - convert Results to Array and wrap as observable */ realm.objects(Lap).sorted("time", ascending: false).asObservableArray() .map {array in array.prefix(5) } .bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) {row, element, cell in cell.textLabel!.text = formatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: element.time)) }.addDisposableTo(bag) /* Use bindable sinks to add objects */ addOneItemButton.rx_tap .map { Lap() } .bindTo(Realm.rx_add()) .addDisposableTo(bag) addTwoItemsButton.rx_tap .map { [Lap(), Lap()] } .bindTo(Realm.rx_add()) .addDisposableTo(bag)
  12. Incremental search [email protected] class ViewController: UIViewController, UISearchBarDelegate, UITableViewDataSource { ...

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return results.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { cell = ... item = results[indexPath.row] cell.textLabel.text = ... return cell } func searchWithText(text: String) { realm .objects(Location) .filter("...") .sorted("...") .addNotificationBlock { (...) in ... } ... tableView.reloadData() } func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { ... searchWithText(text) return true }
  13. Incremental search [email protected] class ViewController: UIViewController { let bag =

    DisposeBag() let realm = try! Realm() @IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() searchBar .rx_text .subscribeNext { [unowned self] (query) in self.realm .objects(Location) .filter("address CONTAINS %@", query) .sorted("identifier") .asObservable() .bindTo(self.tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) {( cell.textLabel!.text = element.address self.tableView.reloadData() }.addDisposableTo(self.bag) } .addDisposableTo(bag) }
  14. Where to find us • ! Realm Japan User Group:

    facebook.com/groups/realmjp • ! Twitter: twitter.com/realmJapan • GitHub: github.com/realm • ! StackOverflow: ja.stackoverflow.com/questions/tagged/realm • ! Email: [email protected] • ! Slack: slack.realm.io/ [email protected] [email protected]