问题检测 RX 中可观察到的 OnCompleted

发布于 2024-11-18 12:28:12 字数 3647 浏览 3 评论 0原文

我在 RX 中检测 onComplited 操作时遇到问题。 我正在从 Web 服务将数据加载到 ListBox。

基本情况运行良好: - “Complited item”位于底部

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
   .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

observable.ObserveOnDispatcher().Subscribe(
    item => { items.Add( new WebServiceClass.ItemGetValues(item.nazwa, item.skrot, item.id) ); },
    () => { items.Add( new WebServiceClass.ItemGetValues("Complited", "", "") ); }
);

但是当我使用下面的代码为每个项目调用 Web 服务时,首先添加“Complited item”

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
   .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

observable.ObserveOnDispatcher().Subscribe
    (item =>
        {
            //items.Add(item);
            var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, client.EndGetLastValue);
            request(item.skrot).ObserveOnDispatcher().Subscribe(
                    (it) =>
                    {
                        Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
                                        ((List<WebServiceClass.ItemGetValues>)JsonConvert
                                        .DeserializeObject(r,
                                            typeof(List<WebServiceClass.ItemGetValues>)))
                                        .First();
                        item.zm_dzienna = deserializeFirst(it).zm_dzienna;
                        items.Add(item);
                    }
            );
        },
    () => { items.Add(new WebServiceClass.ItemGetValues("Complited", "Complited", "0")); }
    );

如何正确检测 onComplited 操作?


解决方案

deserializeFirst 函数中出现 nullReference 异常问题。

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
    .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, client.EndGetLastValue);

observable.SelectMany(
    item => request(item.skrot).Select(it => {
                                                Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
                                                ((List<WebServiceClass.ItemGetValues>)JsonConvert
                                                .DeserializeObject(r,
                                                    typeof(List<WebServiceClass.ItemGetValues>)))
                                                .First();
                                                item.zm_dzienna = deserializeFirst(it).zm_dzienna;
                                                return item; 
                                                })
    ).SubscribeOnDispatcher().Subscribe(
            result => { Dispatcher.BeginInvoke(delegate { items.Add(result); }); },
            () => { Dispatcher.BeginInvoke(delegate { items.Add(new WebServiceClass.ItemGetValues("c","c","c")); }); }
        );

谨致问候, 武卡斯

I have problem with detect onComplited action in RX.
I'm loading data to ListBox from web service.

Basic situation is working great: - "Complited item" is on bottom

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
   .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

observable.ObserveOnDispatcher().Subscribe(
    item => { items.Add( new WebServiceClass.ItemGetValues(item.nazwa, item.skrot, item.id) ); },
    () => { items.Add( new WebServiceClass.ItemGetValues("Complited", "", "") ); }
);

But when I call web service for each item with code below the "Complited item" is added at first

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
   .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

observable.ObserveOnDispatcher().Subscribe
    (item =>
        {
            //items.Add(item);
            var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, client.EndGetLastValue);
            request(item.skrot).ObserveOnDispatcher().Subscribe(
                    (it) =>
                    {
                        Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
                                        ((List<WebServiceClass.ItemGetValues>)JsonConvert
                                        .DeserializeObject(r,
                                            typeof(List<WebServiceClass.ItemGetValues>)))
                                        .First();
                        item.zm_dzienna = deserializeFirst(it).zm_dzienna;
                        items.Add(item);
                    }
            );
        },
    () => { items.Add(new WebServiceClass.ItemGetValues("Complited", "Complited", "0")); }
    );

How can I properly detect onComplited action?


Solution

Problem with nullReference exception in deserializeFirst func.

App.ViewModel.LoadData();

IObservable<WebServiceClass.ItemGetValues> observable = App.ViewModel.Items
    .ToObservable(Scheduler.NewThread);

var items = new ObservableCollection<WebServiceClass.ItemGetValues>();
ListBox1.ItemsSource = items;

var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, client.EndGetLastValue);

observable.SelectMany(
    item => request(item.skrot).Select(it => {
                                                Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
                                                ((List<WebServiceClass.ItemGetValues>)JsonConvert
                                                .DeserializeObject(r,
                                                    typeof(List<WebServiceClass.ItemGetValues>)))
                                                .First();
                                                item.zm_dzienna = deserializeFirst(it).zm_dzienna;
                                                return item; 
                                                })
    ).SubscribeOnDispatcher().Subscribe(
            result => { Dispatcher.BeginInvoke(delegate { items.Add(result); }); },
            () => { Dispatcher.BeginInvoke(delegate { items.Add(new WebServiceClass.ItemGetValues("c","c","c")); }); }
        );

Best regards,
Łukasz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-11-25 12:28:12

您需要将内部查询拉入 Rx 序列,而不是从订阅中运行它。这样做可以让您获得 Rx 的强大功能、可组合性和可测试性。并为您带来您想要的结果。

这是实现所需目标的一个开始。

Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
        ((List<WebServiceClass.ItemGetValues>)JsonConvert
        .DeserializeObject(r,typeof(List<WebServiceClass.ItemGetValues>)))
        .First();
var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, 
                                                          client.EndGetLastValue);
observable
    .SelectMany((item) => request(item.skrot))
    .Select((it) => item.zm_dzienna = deserializeFirst(it).zm_dzienna)
    .ObserveOnDispatcher()
    .Subscribe(
        item => items.Add(item), 
        () => items.Add(new WebServiceClass.ItemGetValues("Complited", "Complited", "0")));

You need to pull the inner query into to Rx sequence, not run it from your subscription. Doing so gives you the power of Rx, of composability and testability. And gets you the results you want.

This is a start towards what is needed.

Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
        ((List<WebServiceClass.ItemGetValues>)JsonConvert
        .DeserializeObject(r,typeof(List<WebServiceClass.ItemGetValues>)))
        .First();
var request = Observable.FromAsyncPattern<string, string>(client.BeginGetLastValue, 
                                                          client.EndGetLastValue);
observable
    .SelectMany((item) => request(item.skrot))
    .Select((it) => item.zm_dzienna = deserializeFirst(it).zm_dzienna)
    .ObserveOnDispatcher()
    .Subscribe(
        item => items.Add(item), 
        () => items.Add(new WebServiceClass.ItemGetValues("Complited", "Complited", "0")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文