1.



let disposeBag = DisposeBag()

// 1
let o1 = Student(score: Variable(1))
let o2 = Student(score: Variable(2))
let o3 = Student(score: Variable(4))

// 2
let student = PublishSubject<Student>()

// 3
student.asObservable()
.flatMapLatest {
$0.score.asObservable()
}
.map {
$0 * 10
}
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)

student.onNext(o1)
student.onNext(o2)

o1.score.value = 3

student.onNext(o3)

o2.score.value = 5

o3.score.value = 6

student.onCompleted()


 

3,materialize


(1)基本介绍




  • 该操作符可以将序列产生的事件,转换成元素。
  • 通常一个有限的 Observable 将产生零个或者多个 onNext 事件,最后产生一个 onCompleted 或者 onError 事件。而 materialize 操作符会将 Observable 产生的这些事件全部转换成元素,然后发送出来。



        AudioProvider

        .fetchAudioBanners()

        .materialize()

        .elements()

        .subscribeNext(weak: self) { (self) in

            return { (resps) in

                print(resps)

            }

        }

        .disposed(by: disposeBag)



let disposeBag = DisposeBag()

let refreshRelay = PublishSubject<Void>()

let refresh = refreshRelay
.asObservable()
.share()

refresh.subscribe {(event) in
print(event)
}.disposed(by: disposeBag)

refreshRelay.onNext(Void())


 

 

 

3.

1)Value of type 'Observable<Event<[MusicSheetInfo]>>' has no member 'elements'



public func elements() -> Observable<E.ElementType> {
return filter { $0.event.element != nil }
.map { $0.event.element! }
}


 

2)subscribeNext



public func subscribeNext<A: AnyObject>(weak obj: A, _ onNext: @escaping (A) -> (Self.E) -> Void) -> Disposable {
return self.subscribe(onNext: weakify(obj, method: onNext))
}


 

 

pod 'RxSwiftExt', '~> 3.4.0'




        let disposeBag = DisposeBag()

        

        let refreshRelay = PublishSubject<Void>()

 

        let refresh = refreshRelay

            .asObservable()

            .share()

        

        let ballad = MusicSheetInfo()

        ballad.type = 23

        ballad.title = "情歌对唱榜"

        ballad.imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554616722369&di=cdfdf3fc4c951c44e40b9d63cad0a2a9&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2F82fe4556b587af3350ff80d56bf803eac661d75f.jpg"

 

        var bannerLoaded: Observable<[MusicSheetInfo]>

        

        let a2 = Observable.just([ballad])

        let a3 =  a2.materialize()

        let bannerReq = refresh

            .flatMap {

                a3

                   

            }.share()

        

//        bannerLoaded = bannerReq.elements()

        

        bannerLoaded = bannerReq.filter { $0.event.element != nil }

        .map { $0.event.element! }

        

        bannerLoaded.subscribe(onNext: { (resps) in

            print("11",resps)

        }).disposed(by: disposeBag)

 

 

//        bannerLoaded

//        .subscribeNext(weak: self) { (self) in

//            return { (resps) in

//                 print("22",resps)

//            }

//        }.disposed(by: disposeBag)

 



        refreshRelay.onNext(Void())


 

Printing description of a2:  <Just<Array<MusicSheetInfo>>: 0x6000019112c0>

Printing description of a3:  <Materialize<Array<MusicSheetInfo>>: 0x60000192c560>

Printing description of bannerReq:  <ShareWhileConnected<Event<Array<MusicSheetInfo>>>: 0x600001709080>

Printing description of bannerLoaded:  <Map<Event<Array<MusicSheetInfo>>, Array<MusicSheetInfo>>: 0x600001709350>

Printing description of resps:

▿ 1 element

  ▿ 0 : <studyRxswift.MusicSheetInfo: 0x600001704030>

 



//        let bannerLoaded = bannerReq
// .filter { $0.event.element != nil }
// .map { $0.event.element! }
// bannerLoaded.subscribe(onNext: { (resps) in
// print("11",resps)
// }).disposed(by: disposeBag)


bannerReq.subscribe(onNext: { (event) in
print("11",event.event.element!)
}).disposed(by: disposeBag)


 4.Moya MultiTarget



extension ZY where Base: MoyaProvider<MultiTarget> {
@discardableResult
open func request(_ target: TargetType, completion: @escaping Moya.Completion) -> Cancellable {
return base.request(MultiTarget(target), queue: nil, completion: completion)
}
@discardableResult
//Post是实现TargetType协议的枚举
func post(_ target: Post, completion: @escaping Moya.Completion) -> Cancellable {
return base.request(MultiTarget(target), queue: nil, completion: completion)
}
@discardableResult
//Get是实现TargetType协议的枚举
func get(_ target: Get, completion: @escaping Moya.Completion) -> Cancellable {
return base.request(MultiTarget(target), queue: nil, completion: completion)
}

}
//TODO:waiting for swift 4 https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#conditional-conformances-
extension MoyaProvider: ZYCompatible /*where Target: MultiTarget*/{}

let publicParamEndpointClosure = { (target: MultiTarget) -> Endpoint<MultiTarget> in
let url = target.path.isEmpty ? target.baseURL.absoluteString : target.baseURL.appendingPathComponent(target.path).absoluteString
let endpoint = Endpoint<MultiTarget>(url: url, sampleResponseClosure: { .networkResponse(200, target.sampleData) }, method: target.method, parameters: target.parameters, parameterEncoding: target.parameterEncoding)
return endpoint.adding(newHTTPHeaderFields: ["Content-Type" : "application/json"])
}

let zyApi:ZY<MoyaProvider<MultiTarget>> = { () -> MoyaProvider<MultiTarget> in
let manager = Manager(configuration: URLSessionConfiguration.default,serverTrustPolicyManager:CustomServerTrustPoliceManager())
let provider = MoyaProvider(endpointClosure: publicParamEndpointClosure, manager:manager,plugins:[NetworkLoggerPlugin(verbose: true)])
return provider
}().zy

class CustomServerTrustPoliceManager : ServerTrustPolicyManager {
override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
return .disableEvaluation
}
public init() {
super.init(policies: [:])
}
}


 

 

​https://www.jianshu.com/p/574201cfd61c​