Skip to content

Instantly share code, notes, and snippets.

View creaaa's full-sized avatar

crea creaaa

  • Fukuoka, JP
View GitHub Profile
func testFuture() {
let expectation1 = XCTestExpectation(description: self.debugDescription)
var cancellables = Set<AnyCancellable>()
let future = Future<Int, Error> { promise in
promise(.success(42))
}
// Future自体は他のPubと全く同じように、Subされて初めて値を放出し始めるのだから、
@creaaa
creaaa / Controlling Publishing with Connectable Publishers
Last active October 20, 2021 02:21
share、makeConnectable、connect
share() を消すとどうなるか?
=> 購読が1本ではなく、2本発生するので、1も2も両方 valueとfinished を無事受け取れる。
Received data 1: 1256 bytes.
Received completion 1: finished.
Received data 2: 1256 bytes.
Received completion 2: finished.
これだとPublisherの生成コストがエコではないから、share()を使ってPublisherを struct => class化し、
//
// ContentView.swift
// SwiftyPageControl
//
// Created by crea on 2019/09/15.
// Copyright © 2019 crea. All rights reserved.
//
import SwiftUI
//
// ContentView.swift
// NaviGomi
//
// Created by crea on 2019/09/09.
// Copyright © 2019 crea. All rights reserved.
//
import SwiftUI
tap "fromkk/testsummaries"
tap "getsentry/tools"
tap "go-delve/delve"
tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/core"
tap "homebrew/services"
tap "mono0926/license-plist"
brew "bitrise"
brew "carthage"
import UIKit
class HogeClass {}
// 意味: このプロトコルに適合させるクラスは、
// 必ずこのクラス(HogeClass)のサブクラスでなくてはならない
// ≒ HogeClassを継承しないといけない
// (Self == HogeClass とは書けない)
protocol HogeProtocol where Self: HogeClass {
func hoge()
@creaaa
creaaa / Single
Last active January 27, 2019 15:54
[Q1]
let single = Single<Int>.create(subscribe: { (observer) in
observer(.success(1))
observer(.success(2))
return Disposables.create()
})
_ = single.subscribe(onSuccess: { print($0) },
onError: { print($0) })
@creaaa
creaaa / empty
Created January 27, 2019 15:14
ちゃんとonCompleted流れとる
let observable = Observable<Int>.empty()
observable
.subscribe(onCompleted: { print("completedきた") })
.disposed(by: bag)
// result
"completedきた"
@creaaa
creaaa / staticなプロパティの生存期間
Last active November 4, 2018 07:01
staticなプロパティの生存期間
import UIKit
class StaticClass {
static var pokemon = Pokemon(name: "Pikachu")
deinit {
print("static class died")
}
}
@creaaa
creaaa / hotかcoldか関係あるのって、Observableの中身までだからな。購読者の中の挙動には関係ない。
Created November 3, 2018 08:49
bossの中のmap(Cold)は、3箇所から購読されてるので3回読まれているが、subscribe onNextの中("hogehoge1"とか)は、それぞれ1回しか実行されないぞ。当たり前かもだが。
let boss = self.tfBoss.rx.text
.map { text -> String in
print("はいきた")
let t = text ?? ""
return t
}
boss.subscribe(onNext: { text in
print("hogehoge1")
self.tf1.text = text