Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add background thread for bitcoin listeners. (#245)
Browse files Browse the repository at this point in the history
- Make Demo App create and start kit on background thread
  • Loading branch information
ant013 committed Feb 18, 2019
1 parent c48dffe commit d55d988
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
27 changes: 22 additions & 5 deletions HSBitcoinKit/HSBitcoinKit/Core/BitcoinKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import RxSwift
public class BitcoinKit {

public weak var delegate: BitcoinKitDelegate?
public var delegateQueue = DispatchQueue(label: "bitcoin_delegate_queue")

private var unspentOutputsNotificationToken: NotificationToken?
private var transactionsNotificationToken: NotificationToken?
Expand Down Expand Up @@ -246,26 +247,42 @@ extension BitcoinKit {
extension BitcoinKit: IDataProviderDelegate {

func transactionsUpdated(inserted: [TransactionInfo], updated: [TransactionInfo]) {
delegate?.transactionsUpdated(bitcoinKit: self, inserted: inserted, updated: updated)
delegateQueue.async { [weak self] in
if let kit = self {
kit.delegate?.transactionsUpdated(bitcoinKit: kit, inserted: inserted, updated: updated)
}
}
}

func transactionsDeleted(hashes: [String]) {
delegate?.transactionsDeleted(hashes: hashes)
delegateQueue.async { [weak self] in
self?.delegate?.transactionsDeleted(hashes: hashes)
}
}

func balanceUpdated(balance: Int) {
delegate?.balanceUpdated(bitcoinKit: self, balance: balance)
delegateQueue.async { [weak self] in
if let kit = self {
kit.delegate?.balanceUpdated(bitcoinKit: kit, balance: balance)
}
}
}

func lastBlockInfoUpdated(lastBlockInfo: BlockInfo) {
delegate?.lastBlockInfoUpdated(bitcoinKit: self, lastBlockInfo: lastBlockInfo)
delegateQueue.async { [weak self] in
if let kit = self {
kit.delegate?.lastBlockInfoUpdated(bitcoinKit: kit, lastBlockInfo: lastBlockInfo)
}
}
}

}

extension BitcoinKit: IKitStateProviderDelegate {
func handleKitStateUpdate(state: KitState) {
delegate?.kitStateUpdated(state: state)
delegateQueue.async { [weak self] in
self?.delegate?.kitStateUpdated(state: state)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions HSBitcoinKit/HSBitcoinKit/Core/DataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class DataProvider {
self.balance = unspentOutputProvider.balance
self.lastBlockInfo = realmFactory.realm.objects(Block.self).sorted(byKeyPath: "height").last.map { blockInfo(fromBlock: $0) }

balanceUpdateSubject.debounce(debounceTime, scheduler: MainScheduler.instance).subscribeAsync(disposeBag: disposeBag, onNext: {
balanceUpdateSubject.debounce(debounceTime, scheduler: ConcurrentDispatchQueueScheduler(qos: .background)).subscribe(onNext: {
self.balance = unspentOutputProvider.balance
self.delegate?.balanceUpdated(balance: self.balance)
})
}).disposed(by: disposeBag)
}

private func transactionInfo(fromTransaction transaction: Transaction) -> TransactionInfo {
Expand Down
2 changes: 1 addition & 1 deletion HSBitcoinKit/HSBitcoinKit/Managers/FeeRateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FeeRateManager {

self.timer.delegate = self

reachabilityManager.subject
reachabilityManager.subject.observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe(onNext: { [weak self] connected in
if connected {
self?.updateFeeRate()
Expand Down
3 changes: 1 addition & 2 deletions HSBitcoinKit/HSBitcoinKit/Managers/FeeRateSyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ extension FeeRateSyncer: IFeeRateSyncer {
var observable = networkManager.getFeeRate()

if async {
observable = observable.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background)).observeOn(MainScheduler.instance)
observable = observable.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
}

observable
.subscribe(onNext: { [weak self] feeRate in
self?.timer.schedule()
Expand Down
21 changes: 17 additions & 4 deletions HSBitcoinKitDemo/HSBitcoinKitDemo/BalanceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ class BalanceController: UIViewController {

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logout))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(start))
navigationItem.rightBarButtonItem?.isEnabled = false

Manager.shared.kitInitializationCompleted.subscribe(onNext: { [weak self] status in
if status {
self?.initializeBitcoinKit()
}
}).disposed(by: disposeBag)
}

func initializeBitcoinKit() {
let bitcoinKit = Manager.shared.bitcoinKit!

update(balance: bitcoinKit.balance)
Expand All @@ -45,6 +54,8 @@ class BalanceController: UIViewController {
Manager.shared.lastBlockInfoSubject.observeOn(MainScheduler.instance).subscribe(onNext: { [weak self] info in
self?.update(lastBlockInfo: info)
}).disposed(by: disposeBag)

navigationItem.rightBarButtonItem?.isEnabled = true
}

@objc func logout() {
Expand All @@ -58,10 +69,12 @@ class BalanceController: UIViewController {
}

@objc func start() {
do {
try Manager.shared.bitcoinKit.start()
} catch {
print("Start Error: \(error)")
DispatchQueue.global(qos: .userInitiated).async {
do {
try Manager.shared.bitcoinKit.start()
} catch {
print("Start Error: \(error)")
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions HSBitcoinKitDemo/HSBitcoinKitDemo/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Manager {

var bitcoinKit: BitcoinKit!

let kitInitializationCompleted = BehaviorSubject<Bool>(value: false)

let balanceSubject = PublishSubject<Int>()
let lastBlockInfoSubject = PublishSubject<BlockInfo>()
let progressSubject = PublishSubject<BitcoinKit.KitState>()
Expand All @@ -36,12 +38,18 @@ class Manager {
}

clearWords()

kitInitializationCompleted.onNext(false)
bitcoinKit = nil
}

private func initWalletKit(words: [String]) {
bitcoinKit = BitcoinKit(withWords: words, coin: coin, walletId: "SomeId", confirmationsThreshold: 1)
bitcoinKit.delegate = self
DispatchQueue.global(qos: .userInitiated).async {
self.bitcoinKit = BitcoinKit(withWords: words, coin: self.coin, walletId: "SomeId", confirmationsThreshold: 1)
self.bitcoinKit.delegate = self

self.kitInitializationCompleted.onNext(true)
}
}

private var savedWords: [String]? {
Expand Down

0 comments on commit d55d988

Please sign in to comment.