Xamarin.ios 如何为ios实现系统反应式调度程序

Xamarin.ios 如何为ios实现系统反应式调度程序,xamarin.ios,system.reactive,Xamarin.ios,System.reactive,我在ios项目中使用System.Reactive,我知道我需要使用ObserveOn来指定在哪个线程上执行订阅服务器。然而,我似乎无法让它正常工作 就我所知,这应该是可行的,还是我实施错了 public class UiContext : IScheduler { /// <inheritdoc /> public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState,

我在ios项目中使用System.Reactive,我知道我需要使用ObserveOn来指定在哪个线程上执行订阅服务器。然而,我似乎无法让它正常工作

就我所知,这应该是可行的,还是我实施错了

public class UiContext : IScheduler
{
    /// <inheritdoc />
    public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
    {
        NSOperationQueue.MainQueue.AddOperation(() => action(this, state));
        return Disposable.Empty;
    }

    /// <inheritdoc />
    public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
    {
        NSOperationQueue.MainQueue.AddOperation(() => action(this, state));
        return Disposable.Empty;
    }

    /// <inheritdoc />
    public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
    {
        NSOperationQueue.MainQueue.AddOperation(() => action(this, state));
        return Disposable.Empty;
    }

    /// <inheritdoc />
    public DateTimeOffset Now { get; }
}

    void SomeMethod()
    {
        WhenValidationChanged
            .ObserveOn(new UiContext())
            .SubscribeOn(new UiContext())
            .Throttle(TimeSpan.FromMilliseconds(50))
            .Subscribe(OnValidationChanged);
    }

    private void OnValidationChanged(object obj)
    {
        if (TableView.DataSource is InfoFieldsDataSource dataSource)
        {
            var validationErrors = dataSource.Items.OfType<InfoFieldViewModelBase>().Count(d => !d.IsValid);
            // Exception is raised about not being executed on UI thread
            _validationController.View.BackgroundColor = validationErrors > 0 ? UIColor.Green : UIColor.Red;
        }
    }
公共类UiContext:isScheduler
{
/// 
公共IDisposable时间表(T状态、Func操作)
{
NSOperationQueue.MainQueue.AddOperation(()=>action(this,state));
返回一次性。空;
}
/// 
公共IDisposable时间表(TState、TimeSpan dueTime、Func action)
{
NSOperationQueue.MainQueue.AddOperation(()=>action(this,state));
返回一次性。空;
}
/// 
公共IDisposable时间表(TState、DateTimeOffset、dueTime、Func操作)
{
NSOperationQueue.MainQueue.AddOperation(()=>action(this,state));
返回一次性。空;
}
/// 
public DateTimeOffset Now{get;}
}
void方法()
{
当验证更改时
.ObserveOn(新UiContext())
.SubscribeOn(新UiContext())
.节气门(时间跨度从毫秒(50))
.订阅(OnValidationChanged);
}
私有void OnValidationChanged(对象obj)
{
if(TableView.DataSource是InfoFieldsDataSource数据源)
{
var validationErrors=dataSource.Items.OfType().Count(d=>!d.IsValid);
//引发关于未在UI线程上执行的异常
_validationController.View.BackgroundColor=validationErrors>0?UIColor.Green:UIColor.Red;
}
}

之前调用
.ObserveOn(new UiContext())
。节流(TimeSpan.frommilluses(50))
可能没有效果,因为
节流可以更改调度程序-每个操作员都可以更改调度程序。您应该始终在操作员或订阅呼叫之前执行
.ObserveOn

之前调用
.ObserveOn(new UiContext())
。Throttle(TimeSpan.frommilluses(50))
可能没有效果,因为
Throttle
可以更改调度程序-每个操作员都可以更改调度程序。您应该始终在操作员或订阅呼叫之前执行
.ObserveOn

之前调用
.ObserveOn(new UiContext())
。Throttle(TimeSpan.frommilluses(50))
可能没有效果,因为
Throttle
可以更改调度程序-每个操作员都可以更改调度程序。您应该总是在您想要申请的接线员或订阅呼叫之前执行
.ObserveOn
。我将在周一确认您的建议,并让您知道它是否有效。谢谢你advance@Enigmativity请将您的评论作为答案,以便我可以将其标记为正确答案。调用的顺序确实导致了此问题。我一直认为这是一种构建器模式,调用顺序无关紧要。谢谢它不是在建一个物体,而是在建一条管道。顺序很重要。在
之前调用
.ObserveOn(new UiContext())
节流(TimeSpan.frommilluses(50))
可能没有效果,因为
节流可以更改计划程序-每个操作员都可以更改计划程序。您应该总是在您想要申请的接线员或订阅呼叫之前执行
.ObserveOn
。我将在周一确认您的建议,并让您知道它是否有效。谢谢你advance@Enigmativity请将您的评论作为答案,以便我可以将其标记为正确答案。调用的顺序确实导致了此问题。我一直认为这是一种构建器模式,调用顺序无关紧要。谢谢它不是在建一个物体,而是在建一条管道。秩序很重要。