Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ Example/Pods

# mac OS
.DS_Store
/Example/Podfile.lock
/Example/RxRealmDataSources.xcworkspace/xcshareddata
184 changes: 82 additions & 102 deletions Example/RxRealmDataSources.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C5B02CE1E1678C80009FBDE"
BuildableName = "RxRealmDataSources_MacExample.app"
BlueprintName = "RxRealmDataSources_MacExample"
ReferencedContainer = "container:RxRealmDataSources.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C5B02CE1E1678C80009FBDE"
BuildableName = "RxRealmDataSources_MacExample.app"
BlueprintName = "RxRealmDataSources_MacExample"
ReferencedContainer = "container:RxRealmDataSources.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C5B02CE1E1678C80009FBDE"
BuildableName = "RxRealmDataSources_MacExample.app"
BlueprintName = "RxRealmDataSources_MacExample"
ReferencedContainer = "container:RxRealmDataSources.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9C5B02CE1E1678C80009FBDE"
BuildableName = "RxRealmDataSources_MacExample.app"
BlueprintName = "RxRealmDataSources_MacExample"
ReferencedContainer = "container:RxRealmDataSources.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
107 changes: 107 additions & 0 deletions Example/RxRealmDataSources/DataRandomizerTree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// DataRandomizerTree.swift
// RxRealmDataSources
//
// Created by Sergiy Vynnychenko on 4/24/18.
// Copyright © 2018 CocoaPods. All rights reserved.
//

import Foundation
import RealmSwift
import RxSwift

class DataRandomizerTree {

private let bag = DisposeBag()
private let rootItem = TreeItem()

lazy var config: Realm.Configuration = {
var config = Realm.Configuration.defaultConfiguration
config.inMemoryIdentifier = UUID().uuidString
return config
}()

private lazy var realm: Realm = {
let realm: Realm
do {
realm = try Realm(configuration: self.config)
return realm
}
catch let e {
print(e)
fatalError()
}
}()

init() {
try! realm.write {
rootItem.title = "1"
rootItem.time = 40
realm.add(rootItem)
}
}

private func insertChild() {
let items = realm.objects(TreeItem.self)
let index = items.count.random()
let parent = items[index]
let newItem = TreeItem()


try! realm.write {
newItem.title = "\(parent.title)+1"
newItem.time = parent.time * 3.14
newItem.parent = parent

realm.add(newItem)
print("tree item: added child for \(parent.title)]")
}
}

private func updateItem() {
let items = realm.objects(TreeItem.self)
let index = items.count.random()
let item = items[index]

try! realm.write {
print("tree item: going to update \(item.title)")
item.title += "+"
}
}

private func deleteItem() {
let items = realm.objects(TreeItem.self).filter("children.@count = 0")
if items.count > 0 {
let item = items[items.count.random()]

let parentTitle = item.parent?.title ?? "root item"
print("tree item: going to delete \(item.title) of parent \(parentTitle)")

try! realm.write {
realm.delete(item)
}
}
}

func start() {
// insert some laps
Observable<Int>.interval(1.0, scheduler: MainScheduler.instance)
.subscribe(onNext: {[weak self] _ in
self?.insertChild()
})
.disposed(by: bag)

Observable<Int>.interval(1.0, scheduler: MainScheduler.instance)
.subscribe(onNext: {[weak self] _ in
self?.updateItem()
})
.disposed(by: bag)

Observable<Int>.interval(2.0, scheduler: MainScheduler.instance)
.subscribe(onNext: {[weak self] _ in
self?.deleteItem()
})
.disposed(by: bag)
}
}

44 changes: 44 additions & 0 deletions Example/RxRealmDataSources/TreeItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// TreeItem.swift
// RxRealmDataSources
//
// Created by Sergiy Vynnychenko on 4/23/18.
// Copyright © 2018 CocoaPods. All rights reserved.
//

import Foundation
import RealmSwift
import RxRealmDataSources

@objcMembers class TreeItem: Object, RxOutlineViewRealmDataItem {

override static func primaryKey() -> String? { return "key" }

dynamic var key : String = UUID().uuidString
dynamic var title : String = ""
dynamic var time : Double = 0
dynamic var children = LinkingObjects(fromType: TreeItem.self, property: "parent")
dynamic var parent : TreeItem? { didSet { _indexInParent = (parent == nil) ? -1 : parent!.childrenCount } }
dynamic var _indexInParent : Int = -1

// RxOutlineViewRealmDataItem implementation
var isExpandable : Bool { return childrenCount > 0 }
var childrenCount : Int { return children.count }
var indexInParent : Int { return _indexInParent}

func childAt(idx: Int) -> RxOutlineViewRealmDataItem? {
guard idx >= 0 else { return nil }
guard idx < children.count else { return nil }

return children[idx]
}

func getParent() -> RxOutlineViewRealmDataItem? {
return parent
}

func getChildren() -> [RxOutlineViewRealmDataItem] {
return children.toArray()
}
}

Loading