UniRx SchedulerOn MainThread 在第一次调用时不起作用
UniRx SchedulerOn MainThread not works at first time invoke
我在我的项目中大量使用 UniRx,但是当我第一次调用 SchedulerOn(Scheduler.MainThread)
时我发现了一个问题,它不会像这样在主要 thread.Uses 代码中做遗留的事情:
Login (new LoginReq (account.text, pwd.text, 1L)).SchedulerOn(Scheduler.MainThread).Done (x => {
Debug.Log("login req thread - " + System.Threading.Thread.CurrentThread.ManagedThreadId);
Package.Log("Login response - " + x.ToString());
if(x.errorMsg == null) {
Package.Log("login success - " + x.ToString());
//choice game world -> choice role
enterObj.SetActive(true);
loginObj.SetActive(false);
} else {
rspTips.text = x.ToString();
}
});
输出显示 SetActive 只能在主线程调用。
当我打开 PC 并首先 运行 应用程序时,问题特别发生。
是因为 .NET 预热了吗?我不确定线程调度是如何工作的。
更新
我想我失去了一些重要的东西。
Login
return RSG.IPromise<T>
(git home page at here) which used to lightweight asyc task but RSG.Promise
lib doesn't have scheduler module.So I'm wrap a SchedulerPromise
with UniRx
's IScheduler
and the code not has novel technology.See this please about the wrapper source code
我在这里呆了很长时间time.Thanks任何建议。
UniRx 在单独的线程上运行其调度,但 Unity 是单线程的并且不是线程安全的。它阻止代码在单独的线程上从 运行 访问其对象。
UniRX 提供了一种方法来将对 observable 的订阅加入到其主线程中,以允许 运行 访问 Unity 对象的代码:
.ObserveOnMainThread()
我在我的项目中大量使用 UniRx,但是当我第一次调用 SchedulerOn(Scheduler.MainThread)
时我发现了一个问题,它不会像这样在主要 thread.Uses 代码中做遗留的事情:
Login (new LoginReq (account.text, pwd.text, 1L)).SchedulerOn(Scheduler.MainThread).Done (x => {
Debug.Log("login req thread - " + System.Threading.Thread.CurrentThread.ManagedThreadId);
Package.Log("Login response - " + x.ToString());
if(x.errorMsg == null) {
Package.Log("login success - " + x.ToString());
//choice game world -> choice role
enterObj.SetActive(true);
loginObj.SetActive(false);
} else {
rspTips.text = x.ToString();
}
});
输出显示 SetActive 只能在主线程调用。
当我打开 PC 并首先 运行 应用程序时,问题特别发生。
是因为 .NET 预热了吗?我不确定线程调度是如何工作的。
更新
我想我失去了一些重要的东西。
Login
return RSG.IPromise<T>
(git home page at here) which used to lightweight asyc task but RSG.Promise
lib doesn't have scheduler module.So I'm wrap a SchedulerPromise
with UniRx
's IScheduler
and the code not has novel technology.See this please about the wrapper source code
我在这里呆了很长时间time.Thanks任何建议。
UniRx 在单独的线程上运行其调度,但 Unity 是单线程的并且不是线程安全的。它阻止代码在单独的线程上从 运行 访问其对象。
UniRX 提供了一种方法来将对 observable 的订阅加入到其主线程中,以允许 运行 访问 Unity 对象的代码:
.ObserveOnMainThread()