Share
Embed
Transcript
37 slides
-
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] -
Realm Objective-C/Swift 1.0.2 • ϓϩύςΟ͕ଘࡏ͠ͳ͍ΫϥεΛอଘ͠Α͏ͱ͢Δͱྫ֎ʹ • write(_:)ϝιουͷϒϩοΫͰthrowͰ͖ΔΑ͏ʹ • όΠφϦαΠζ͕গ͠ݮগ •
addNotificationBlock(_:, change:)Ͱॳճͷݺͼग़͠ඞ ͣ.Initial͕ݺΕΔΑ͏ʹ • ٯํͷؔ࿈͕มߋʹͳΔ߹ʹ௨͕ݺΕͳ͍Λमਖ਼ [email protected] [email protected] -
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 -
What’s Rx? [email protected] [1, 2, 3, 4, 5, 6] .filter
{ $0 % 2 == 0 } -
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() } } } -
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() } } } -
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() } } } -
RxRealm makes Realm observable [email protected] public extension NotificationEmitter where Self:
RealmCollectionType { public func asObservable() -> Observable<Self> { return Observable.create {...} } } -
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 {} -
RxRealm makes Realm observable [email protected] public extension NotificationEmitter where Self:
RealmCollectionType { public func asObservable() -> Observable<Self> { return Observable.create {...} } } -
Usage [email protected] var token: NotificationToken? func searchWithText(text: String) { token
= realm .objects(Location) .filter("...") .sorted("...") .addNotificationBlock { (...) in ... tableView.reloadData() } ... } -
Usage [email protected] realm .objects(Location) .filter("address CONTAINS %@", query) .sorted("identifier") .asObservable()
.subscribeNext {[unowned self]results in ... }.addDisposableTo(self.bag) -
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) -
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 } -
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) } -
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]