Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to concatenate the outputs of several async methods into a single source

I am writing several APIs that will parse data from 3rd party sources and return a standard typed item.

I played with the idea of firing events for each item but it appears that each API could potentially yield many (maybe thousands) of items in any single moment (but not continuously).

I now have the task of implementing async methods that yield Enumerable<item> continuously. I haven't implemented async before but there's plenty of tutorials out there.

I would like to have all of these results concatenated into a single source which I will then be able to adapt my awesome Reactive Extension code to, but I don't know how to do that >_<

I tried this:
C#
public IEnumerable<FlightData> GetFlighData()
{
    var trackers = TrackerApiLoader.LoadAsyncTrackers();
    return trackers.SelectMany(t => t.GetDataMethod());
}


I haven't implemented the async / await methods yet but the above code only seems to get data from the first assembly in the list of trackers.

My tracker apis are just returning test data atm.

What is the correct way to get this working?

Thanks ^_^


Update:
Perhaps this will give a better idea of what I am trying to achieve :

C#
public IEnumerable<FlightData> GetFlighData()
{
    //messageQueue deals with synced trackers and itself works like an async tracker
    AsyncTracker messageQueue = TrackerApiLoader.LoadMessageQueue();

    //Load all the AsyncTrackers
    IEnumerable<AsyncTracker> trackers = TrackerApiLoader.LoadAsyncTrackers();

    IObservable<IEnumerable<FlightData>> ob = messageQueue.GetFlighDataMethod().ToObservable();

    //Merge all the methods into one 'hot' observable
    trackers.ToList().ForEach(t =>
    {
        ob.Merge(t.GetFlighDataMethod().ToObservable());
    });

    //I want to deal with each item returned, when it is returned.  This probably wouldn't work though, would it?
    IObservable<FlightData> obs = ob.SelectMany(fd =>
    {
        var enumerable = fd as FlightData[] ?? fd.ToArray();
        return enumerable;
    });

    If I could yield the results, that'd be great, but I can't do this! >_<
    obs.Subscribe(delegate(FlightData data) { yield return data; });

    yield break;
}
Posted
Updated 31-Mar-15 5:07am
v4
Comments
Andy Lanng 31-Mar-15 10:17am    
Hmm - maybe I should yield results instead?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900