HedgehogNSK
HedgehogNSK

Reputation: 153

C# Return variable as observable

I have method that load some character data from outer db to inner db (infoBase) that looks like some kind of list of CharacterInfo objects. The method returns observable. But if user tries to load character data that is already loaded I wish to give the data straight to user from inner db.

    public IObservable<IEnumerable<CharacterInfo>> LoadCharactersOverviewAsync(CharacterInfoBase infoBase, params int[] char_id)
    {

        //Filter that search ids of characters that weren't load yet
        IEnumerable<int> char_id_filtered = ... ;    

        //If list is empty than it means that all we need is already loaded
        if (!char_id_filtered.Any())
          {
            //get list of necessary CharacterInfos from inner db
            IEnumerable<CharacterInfo> result = infoBase.Where(...);
            //Crunch. Don't know hot to return 1 collection as observable
            return Observable.FromCoroutineValue<IEnumerable<CharacterInfo>>(()=> Result(result));
          }
        ...

        //Returns observable result
        return JsonRequestExecutor.GetRequest(requestParameters)
            .Select(json =>
            {
                ...
            }
        );

    }

So as you see I made crunch that creates observable from coroutine which I made just to return result

IEnumerator Result(IEnumerable<CharacterInfo> result)
    {
        yield return result;
    }

Tell me please, how to make it better? P.S. Sorry for my english

Upvotes: 3

Views: 1292

Answers (1)

HedgehogNSK
HedgehogNSK

Reputation: 153

I found the answer by myself. In UniRX there is no operator Observable.Just http://reactivex.io/documentation/operators/just.html But there is operator Start http://reactivex.io/documentation/operators/start.html

So here is the result. Maybe it's not ideal but it's quiet better then what I'v had before

 if (!char_id_filtered.Any())
      {
        //get list of necessary CharacterInfos from inner db
        IEnumerable<CharacterInfo> result = infoBase.Where(...);
        //Here is the result
        return Observable.Start(() =>result);
      }

Update. There is correct way to return single item:

Observable.Return<T>(result);

Upvotes: 1

Related Questions