How to preform operation which returns single for each element of list returned by observable and return it as a list? - rx-java2

The question is quite complicated but in code seems to look simpler (I hope)
I have a functions like bellow
fun trackFoos(): Observable<List<Foo>> {/*....*/}
fun getBarForFoo(fooState: Foo.State): Single<Bar>
And I was having some code which is bound to other parts of apps so I cannot change it too much.
Observable.combineLatest(
trackFoos(),
Observable.interval(1, TimeUnit.SECONDS),
BiFunction() {foos: List<Foo>, _:Long -> foos}
).subscribe { foos -> /*....*/ }
And now I need to pass also object of Bar for each foo object using this getBarForFoo
So I need to write trackPairOfFooAndBar() using trackFoos() and getBarForFoo() to use it as below:
Observable.combineLatest(
trackPairOfFooAndBar(),
Observable.interval(1, TimeUnit.SECONDS),
BiFunction() {fooBarList: List<Pair<Foo,Bar>>, _:Long -> fooBarList}
).subscribe { fooBarList-> /*....*/ }
I saw this answer: https://stackoverflow.com/a/47676208/1461568 but there is assumption that second call is observable (I have Single)
So how to do it?

You can do it like this:
Transform trackFoos() from Observable<List<Foo>> to Observable<Foo>
For each Foo do getBarForFoo() and map result so it really returns Pair<Foo, Bar>
(Optional) If you need Single<List<Pair<Foo,Bar>>> you can just call toList()
(Optional) If you really need Observable then you can just call toObservable()
In code:
fun trackPairOfFooAndBar() : Observable<Pair<Foo, Bar>> {
return trackFoos()
.flatMapIterable { it } // or flatMap {list -> Observable.fromIterable(list)}
.flatMapSingle { foo -> getBarForFoo(foo.state).map { foo to it } }
// optional, depending what is the final signature that you want
//.toList().toObservable
}

Related

Generic type not preserved when called within another generic function

[Updated with a less contrived example]
I'm trying to extend a generic function to provide different behavior for a specific type. This works as expected when I call the function directly. But If I call it from within another generic function, the original generic type is not preserved and I get the default behavior. I'm a bit new to Swift, so I may be missing something obvious here.
My code looks something like this:
protocol Task {
associatedtype Result
}
struct SpecificTask: Task {
typealias Result = String
// other taks related properties
}
enum TaskRunner<T: Task> {
static func run(task: T) throws -> T.Result {
// Task not supported
throw SomeError.error
}
}
extension TaskRunner where T == SpecificTask {
static func run(task: T) throws -> T.Result {
// execute a SpecificTask
return "Some Result"
}
}
func run<T: Task>(task: T) throws -> T.Result {
// Additional logic related to running the task
return try TaskRunner.run(task: task)
}
print(try TaskRunner.run(task: SpecificTask())) // Prints "Some Result"
print(try run(task: SpecificTask())) // Throws SomeError
I need the top-level run function to call the SpecificTask version of the lower-level run() function, but the generic version of the function is called instead
You're trying to reinvent class inheritance with generics. That is not what generics are for, and they don't work that way. Generic methods are statically dispatched, which means that the code is chosen at compile-time, not runtime. An overload should never change the behavior of the function (which is what you're trying to do here). Overrides in where clauses can be used to improve performance, but they cannot be used to create dynamic (runtime) dispatch.
If you must use inheritance, then you must use classes. That said, the problem you've described is better solved with a generic Task rather than a protocol. For example:
struct Task<Result> {
let execute: () throws -> Result
}
enum TaskRunner {
static func run<Result>(task: Task<Result>) throws -> Result {
try task.execute()
}
}
let specificTask = Task(execute: { "Some Result" })
print(try TaskRunner.run(task: specificTask)) // Prints "Some Result"
Notice how this eliminates the "task not supported" case. Rather than being a runtime error, it is now a compile-time error. You can no longer call this incorrectly, so you don't have to check for that case.
If you really want dynamic dispatch, it is possible, but you must implement it as dynamic dispatch, not overloads.
enum TaskRunner<T: Task> {
static func run(task: T) throws -> T.Result {
switch task {
case is SpecificTask:
// execute a SpecificTask
return "Some Result" as! T.Result // <=== This is very fragile
default:
throw SomeError.error
}
}
}
This is fragile because of the as! T.Result. If you change the result type of SpecificTask to something other than String, it'll crash. But the important point is the case is SpecificTask, which is determined at runtime (dynamic dispatch). If you need task, and I assume you do, you'd swap that with if let task = task as? SpecificTask.
Before going down that road, I'd reconsider the design and see how this will really be called. Since the Result type is generic, you can't call arbitrary Tasks in a loop (since all the return values have to match). So it makes me wonder what kind of code can actually call run.

Chained generic type inferred closures in swift

I am struggling to conceptualise in my head how to create a generic interface or class which functions
like so.
functionOne {
return "123"
}
.chainedFunction { string -> Int in
return Int(string) + 456
}
.anotherChain {
[NSNumber($0), String($0)]
}
.lastInTheChain {
print("value 1: \($0[0])")
print("value 2: \($0[1])")
}
so for this to work it is essentially an array of functions that pipe its return value into the
next function's parameter list.
assuming each function closure was named after the function it came from
i.e
functionOne(_ functionOne:(T) -> T) {}
chainedFunction(_ chainedFunction:(T) -> T) {}
then you would then call them like so to make sure you pass the correct values through.
lastInTheChain(anotherChain(chainedFunction(functionOne())))
The part i struggly mostly with is the Generics part and whether type inference will work or not
(this is a secondary concern, would just be nice if it worked)
Im also attempting to use protocols, and generic protocols in swift require the use of thunks here and there which is only compounding my confusion.
So anyone out there a tad more knowledgeable if you would not mind chipping in how i could do this or whether it is doable in the first place i would be most appreciative.
functionOne needs to return something that has a chainedFunction instance method, which itself needs to return something that has an anotherChain instance method, and so on. For this, you need to create a type. I'll call it wrapper because I don't know your use case, but you should name it something more meaningful.
import Foundation
struct Wrapper<T> {
let value: T
// This replaces `function1`
init(_ value: T) { self.value = value }
func chainedFunction<R>(_ transform: (T) -> R) -> Wrapper<R> {
return Wrapper<R>(transform(self.value))
}
func anotherChain<R>(_ transform: (T) -> R) -> Wrapper<R> {
return Wrapper<R>(transform(self.value))
}
func lastInTheChain<R>(_ transform: (T) -> R) -> R {
return transform(self.value)
}
}
Wrapper("123")
.chainedFunction { string -> Int in
return Int(string)! + 456
}
.anotherChain {
[NSNumber(value: $0), String($0)]
}
.lastInTheChain {
print("value 1: \($0[0])")
print("value 2: \($0[1])")
}
Terminology: this Wrapper type is called a functor, because it defines a map method (here called chainedFunction and anotherChain, which are equivalent functions in the example). Using map, a closure which can be used to transform T to R (transform) can be used to transform a Wrapper<T> into a Wrapper<R>.

RxSwift: Nested Queries and ReplaySubject

I have to fetch three types of data (AType, BType, CType) using three separate API requests. The objects returned by the APIs are related by one-to-many:
1 AType object is parent of N BType objects
1 BType object is parent of P CType objects)
I'm using the following three functions to fetch each type:
func get_A_objects() -> Observable<AType> { /* code here */ }
func get_B_objects(a_parentid:Int) -> Observable<BType> { /* code here */}
func get_C_objects(b_parentid:Int) -> Observable<CType> { /* code here */}
and to avoid nested subscriptions, these three functions are chained using flatMap:
func getAll() -> Observable<CType> {
return self.get_A_objects()
.flatMap { (aa:AType) in return get_B_objects(aa.id) }
.flatMap { (bb:BType) in return get_C_objects(bb.id) }
}
func setup() {
self.getAll().subscribeNext { _ in
print ("One more item fetched")
}
}
The above code works fine, when there are M objects of AType, I could see the text "One more item fetched" printed MxNxP times.
I'd like to setup the getAll() function to deliver status updates throughout the chain using ReplaySubject<String>. My initial thought is to write something like:
func getAll() -> ReplaySubject<String> {
let msg = ReplaySubject<String>.createUnbounded()
self.get_A_objects().doOnNext { aobj in msg.onNext ("Fetching A \(aobj)") }
.flatMap { (aa:AType) in
return get_B_objects(aa.id).doOnNext { bobj in msg.onNext ("Fetching B \(bobj)") }
}
.flatMap { (bb:BType) in
return get_C_objects(bb.id).doOnNext { cobj in msg.onNext ("Fetching C \(cobj)") }
}
return msg
}
but this attempt failed, i.e., the following print() does not print anything.
getAll().subscribeNext {
print ($0)
}
How should I rewrite my logic?
Problem
It's because you're not retaining your Disposables, so they're being deallocated immediately, and thus do nothing.
In getAll, you create an Observable<AType> via get_A_objects(), yet it is not added to a DisposeBag. When it goes out of scope (at the end of the func), it will be deallocated. So { aobj in msg.onNext ("Fetching A \(aobj)") } will never happen (or at least isn't likely to, if it's async).
Also, you aren't retaining the ReplaySubject<String> returned from getAll().subscribeNext either. So for the same reason, this would also be a deal-breaker.
Solution
Since you want two Observables: one for the actual final results (Observable<CType>), and one for the progress status (ReplaySubject<String>), you should return both from your getAll() function, so that both can be "owned", and their lifetime managed.
func getAll() -> (Observable<CType>, ReplaySubject<String>) {
let progress = ReplaySubject<String>.createUnbounded()
let results = self.get_A_objects()......
return (results, progress)
}
let (results, progress) = getAll()
progress
.subscribeNext {
print ($0)
}
.addDisposableTo(disposeBag)
results
.subscribeNext {
print ($0)
}
.addDisposableTo(disposeBag)
Some notes:
You shouldn't need to use createUnbounded, which could be dangerous if you aren't careful.
You probably don't really want to use ReplaySubject at all, since it would be a lie to say that you're "fetching" something later if someone subscribes after, and gets an old progress status message. Consider using PublishSubject.
If you follow the above recommendation, then you just need to make sure that you subscribe to progress before results to be sure that you don't miss any progress status messages, since the output won't be buffered anymore.
Also, just my opinion, but I would re-word "Fetching X Y" to something else, since you aren't "fetching", but you have already "fetched" it.

RxSwift: Observe rx_text & rx_offset simultaneously

I've been working on a search controller that is using RxSwift to update DataSource when user types in Search field, like it's described here: http://www.thedroidsonroids.com/blog/ios/rxswift-examples-3-networking/
That is my viewmodel:
struct SearchControllerViewModel {
let provider: RxMoyaProvider<ThreadifyEndpoint>
let startsWith: Observable<String>
func startSearching() -> Observable<[SearchedNodeViewModel]> {
return startsWith
.observeOn(MainScheduler.instance)
.flatMapLatest { query -> Observable<[SearchedNodeViewModel]?> in
return self.findNodes(query)
}.replaceNilWith([])
}
internal func findNodes(startsWith: String) -> Observable<[SearchedNodeViewModel]?> {
return self.provider
.request(ThreadifyEndpoint.SearchForNodes(startsWith: startsWith))
.mapArrayOptional(SearchedNodeViewModel.self)
}
}
Now I want new data to be loaded not only when user is typing but either when sh's scrolling down.
I was thinking to use combineLatest to observe both rx_text and rx_offset but I can't pass Observable to combineLatest because of compilation error.
The compilation error you're seeing is due to you not using an actual method. The method signature you're intending to use is:
public class func combineLatest<O1 : ObservableType, O2 : ObservableType>(source1: O1, _ source2: O2, resultSelector: (O1.E, O2.E) throws -> Element) -> RxSwift.Observable<Element>
Notice that there's a third argument that you're forgetting: resultSelector. That's supposed to be a block that describes how you want to combine the latest elements into a new element.
Based on your error message, I'm thinking you're using it like this:
let combined = Observable.combineLatest(stringObservable, pointObservable)
Whereas, you should be using it like this:
let combined = Observable.combineLatest(stringObservable, pointObservable) { (s, p) in
return "\(s), \(p)" // or construct your new element however you'd like
}
Without the block, RxSwift doesn't know how you'd like to combine them. You might have been thinking it would just default to making a new tuple of (String, CGPoint) as the element, but it makes no such assumptions and requires you to tell it.

How do I declare, create, and use method pointers in Swift?

I'm not talking about pointers to C functions, but to a method within a Swift type.
struct Test: GeneratorType {
var methodPointer: mutating () -> Bool? // Non-working guess
var which: Bool
init() {
which = false
methodPointer = which ? &testMethod1 : &testMethod2 // Also non-working guess
}
//...
}
The compiler says "mutating" isn't legal as part of a function declaration. (Actually, it just suggests a semi-colon there.) And for the pointer initialization (after I remove mutating), the compiler thinks I'm trying to call the functions and use their results instead. I want to use the methods as objects in-and-of themselves here, not as a function call. Later on I want to use the pointed-to method within next; without figuring this out, I'll have to resort to an enumeration flag and manually choosing which method to call within next.
I hope there's some part of closure mechanics that allows this. Maybe something like this page, which describes functions returning functions. But none of the examples I've seen mention mutating methods.
See if this helps you.
class Something {
var f: ( () -> Int )?
let f1 = { () -> Int in /* do some action here */ return 1}
let f2 = { () -> Int in /* do some action here */ return 2}
func ff(which: Bool) {
f = which ? f1 : f2
}
func act() {
if let f = f {
f()
}
}
}
Here is how I do it -
class FcnArgs { //#goal pass a fcn as arg
class func demo() {
let fPtr = funcToPointTo; //#type '((Int)->String)'
print(fPtr(4));
}
class func funcToPointTo(_ i : Int) -> String {
print("I Was passed \(i)");
return "I was returned";
}
}
FcnArgs.demo() output:
I Was passed 4
I was returned