{"id":473165,"date":"2025-09-02T15:41:13","date_gmt":"2025-09-02T15:41:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=473165"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=473165","title":{"rendered":"<span>flowOn vs withContext: \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 Flow \u0432 Kotlin<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a <strong>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0432\u00a0Kotlin Flow<\/strong> \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043d\u0435\u00a0\u0442\u043e\u00a0\u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e <code>withContext<\/code>. <\/p>\n<h4>\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b<\/h4>\n<p>\u0412\u00a0Kotlin\u2011\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u0430\u043c\u0438 \u043f\u043e\u00a0\u0441\u0435\u0431\u0435 \u043d\u0435\u0434\u043e\u0440\u043e\u0433\u0438\u0435\u00a0\u2014 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u0442\u044b\u0441\u044f\u0447. \u041d\u043e\u00a0\u0441\u0430\u043c\u0430 \u043f\u043e\u00a0\u0441\u0435\u0431\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043d\u0435\u00a0\u0434\u0430\u0451\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439, \u0447\u0442\u043e\u00a0\u043e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u00a0\u043d\u0443\u0436\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435. \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 (\u0442\u043e \u0435\u0441\u0442\u044c <code>CoroutineDispatcher<\/code>)\u00a0\u2014 \u044d\u0442\u043e <em>\u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439<\/em> \u0432\u044b\u0431\u043e\u0440, \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u0442\u0440\u0435\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u043a\u043e\u0434. \u0418 \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 <code>flow<\/code> \u0438\u0437\u00a0UI, \u043d\u043e\u00a0\u043d\u0435\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 <code>Dispatchers.IO<\/code>, \u0432\u0435\u0441\u044c \u043a\u043e\u0434, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a\u00a0\u0431\u0430\u0437\u0435 \u0438\u043b\u0438\u00a0\u0444\u0430\u0439\u043b\u043e\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443, \u043f\u043e\u0435\u0434\u0435\u0442 \u0432\u00a0\u0433\u043b\u0430\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. <\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442\u00a0\u2014 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u043e\u0442\u043e\u043a\u0430. Kotlin Flow \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430\u00a0\u0442\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0451 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u043b\u043e\u0441\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u0432\u00a0\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435. \u0415\u0441\u043b\u0438 \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u0432\u043e\u0442\u043a\u043d\u0443\u0442\u044c <code>withContext<\/code> \u0438\u043b\u0438 <code>emit()<\/code> \u0438\u0437\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440\u0430\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 <code>IllegalStateException: Flow invariant is violated<\/code>. \u042d\u0442\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u0430\u043a\u00a0\u0440\u0430\u0437 \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442: \u00ab\u0442\u044b \u043d\u0430\u0440\u0443\u0448\u0438\u043b \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u00a0\u2014 emit \u043f\u043e\u0448\u0451\u043b \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430\u00bb. \u0420\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c\u00a0\u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>flowOn<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0431\u0443\u0444\u0435\u0440 \u0438 \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044f\u0435\u0442 upstream \u043e\u0442\u00a0downstream.<\/p>\n<p>\u041d\u0443 \u0438 \u0442\u0440\u0435\u0442\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442\u00a0\u2014 \u0433\u043e\u043d\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u0432\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u044d\u043c\u0438\u0442\u0430 \u0432\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0438\u0445 \u0432\u00a0\u043e\u0431\u0449\u0438\u0439 <code>StateFlow<\/code>, \u043f\u0440\u0438\u00a0\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0439\u043c\u0430\u0442\u044c \u0433\u043e\u043d\u043a\u0443: \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u041d\u0430\u00a0\u0442\u0435\u0441\u0442\u0430\u0445 \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e, \u0430\u00a0\u0432\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0440\u0435\u0434\u043a\u0438\u0439 \u0431\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0443\u0434\u043d\u043e \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0443\u0436\u043d\u043e \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e, \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0438 \u0447\u0435\u0440\u0435\u0437 <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439, \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431.<\/p>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 flowOn<\/h3>\n<p><code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 <strong>\u0433\u0434\u0435 \u0438 \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0430\u043f\u0441\u0442\u0440\u0438\u043c<\/strong>. <em>\u0410\u043f\u0441\u0442\u0440\u0438\u043c<\/em>\u00a0\u2014 \u044d\u0442\u043e \u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0438\u0434\u0451\u0442 <strong>\u0432\u044b\u0448\u0435<\/strong> \u043f\u043e\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u043f\u043e\u0441\u043b\u0435 <code>flow {}<\/code> \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0433\u043e \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0435\u0433\u043e <code>Flow<\/code>. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 <code>flowOn(Dispatchers.IO)<\/code>, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442: \u00ab\u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0432\u044b\u0448\u0435\u00a0\u2014 <code>emit<\/code>, <code>map<\/code>, <code>filter<\/code> \u0438 \u043f\u0440\u043e\u0447\u0435\u0435\u00a0\u2014 \u043f\u0443\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 <code>Dispatchers.IO<\/code>\u00bb. \u0410\u00a0\u0432\u043e\u0442 \u0432\u0441\u0451, \u0447\u0442\u043e <strong>\u043d\u0438\u0436\u0435<\/strong>, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435 <code>flowOn<\/code>, \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u043e\u0440.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"kotlin\">val usersFlow = flow {     logThread(\"start emit\")           emit(loadUsers())             }.flowOn(Dispatchers.IO)           .map { user -&gt; enrich(user) }   <\/code><\/pre>\n<p><em>\u0427\u0442\u043e\u00a0\u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0451\u0442:<\/em><\/p>\n<ul>\n<li>\n<p><code>loadUsers()<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d <strong>\u043d\u0430\u00a0IO<\/strong><\/p>\n<\/li>\n<li>\n<p><code>map { enrich(...) }<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0443\u0436\u0435 <strong>\u0432\u00a0\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u0432\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u0438\u0442\u0435 flow<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>Main<\/code> \u0438\u043b\u0438 <code>Default<\/code>)<\/p>\n<\/li>\n<\/ul>\n<p>\u042d\u0442\u0430 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u00a0\u2014 <em>\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u043e\u043c \u0438 \u0434\u0430\u0443\u043d\u0441\u0442\u0440\u0438\u043c\u043e\u043c<\/em>\u00a0\u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0430\u0432\u043a\u0443 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430. \u0418\u043c\u0435\u043d\u043d\u043e <code>flowOn<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0430\u0443\u0437\u0443 \u043c\u0435\u0436\u0434\u0443 \u0447\u0430\u0441\u0442\u044f\u043c\u0438 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0438 \u043d\u0435\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u0443 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0443\u043d\u0441\u0442\u0440\u0438\u043c. \u0415\u0441\u043b\u0438 <code>loadUsers()<\/code> \u0432\u0434\u0440\u0443\u0433 \u043d\u0430\u0447\u043d\u0451\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c, \u044d\u0442\u043e \u043d\u0435\u00a0\u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 <code>collect<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0435\u0441\u0442\u044c \u0431\u0443\u0444\u0435\u0440. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>flowOn<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e \u0441\u00a0\u0442\u044f\u0436\u0451\u043b\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e <code>flowOn<\/code> \u0432\u043b\u0438\u044f\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0432\u044b\u0448\u0435 \u0441\u0435\u0431\u044f<\/strong>. \u041e\u043d \u043d\u0435\u00a0\u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u0444\u043b\u043e\u0443 \u0446\u0435\u043b\u0438\u043a\u043e\u043c. \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"kotlin\">flow {     emit(doSomething()) }.map { heavyCompute(it) }  .flowOn(Dispatchers.Default)<\/code><\/pre>\n<p><code>heavyCompute<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f <strong>\u043d\u0435<\/strong> \u043d\u0430 <code>Dispatchers.Default<\/code>, \u0430\u00a0\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u0438\u0434\u0451\u0442 <code>collect<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>flowOn<\/code> \u0432\u043b\u0438\u044f\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 <\/strong><code><strong>emit<\/strong><\/code><strong> \u0438 \u0432\u0441\u0451 \u0432\u044b\u0448\u0435<\/strong>. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0438 <code>emit<\/code>, \u0438 <code>map<\/code>\u00a0\u0431\u044b\u043b\u0438 \u043d\u0430 <code>Default<\/code>, \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>flowOn<\/code> \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"kotlin\">flow {     emit(doSomething()) }     .flowOn(Dispatchers.IO)     .map { heavyCompute(it) }     .flowOn(Dispatchers.Default)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>flowOn<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u00a0\u2014 \u0438 \u044d\u0442\u043e \u043a\u0430\u043a\u00a0\u0440\u0430\u0437 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0433\u0438\u0431\u043a\u0438\u0435 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u044b, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u043c \u043f\u0443\u043b\u0435. \u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0442\u043e, \u0447\u0442\u043e\u00a0\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f <code>flowOn<\/code> \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437\u00a0\u043d\u0438\u0445 \u0432\u043b\u0438\u044f\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 <strong>\u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0432\u044b\u0448\u0435<\/strong>.<\/p>\n<p><code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e <strong>\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 <\/strong><code><strong>Flow<\/strong><\/code>, \u0431\u0435\u0437\u00a0\u0440\u0438\u0441\u043a\u0430 \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043b\u0438\u00a0\u043d\u0430\u0440\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>IllegalStateException<\/code>. <\/p>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 withContext (\u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u043d\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)<\/h3>\n<p><code>withContext<\/code>\u00a0\u2014 \u044d\u0442\u043e <strong>suspend\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u044f<\/strong>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0432\u00a0\u0434\u0440\u0443\u0433\u043e\u0439 <code>CoroutineContext<\/code>. \u041e\u043d\u0430 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f\u00a0\u0437\u0430\u0434\u0430\u0447 \u0432\u0440\u043e\u0434\u0435 <code>withContext(Dispatchers.IO) { readFromDisk() }<\/code>, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u0443\u044e \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432\u00a0\u043d\u0443\u0436\u043d\u043e\u043c \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440\u0435. \u041d\u043e\u00a0\u0432\u043d\u0443\u0442\u0440\u0438 <code>Flow<\/code> \u0432\u0441\u0451 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u00a0\u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 <code>withContext<\/code> <strong>\u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0435\u043b\u0430 <\/strong><code><strong>flow {}<\/strong><\/code><strong> \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0438\u043f\u0430 <\/strong><code><strong>map<\/strong><\/code>, \u0432\u044b \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0435\u043d\u044f\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u0412\u044b <strong>\u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0435 <\/strong><code><strong>suspend<\/strong><\/code><strong>\u2011\u0442\u043e\u0447\u043a\u0443 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442<\/strong> \u0438 <strong>\u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435 \u0442\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437<\/strong>, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0435\u0441\u043b\u0438 \u0443\u00a0\u0432\u0430\u0441 \u0442\u044b\u0441\u044f\u0447\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\u00a0\u2014 \u0432\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0442\u044b\u0441\u044f\u0447\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u0442\u0440\u0435\u0434\u043e\u0432. \u0410\u00a0\u044d\u0442\u043e \u0443\u0436\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430.<\/p>\n<pre><code class=\"kotlin\">val wrongFlow = flow {     (1..1000).forEach { id -&gt;         emit(withContext(Dispatchers.IO) {             loadById(id)         })     } }<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u0432\u0438\u043d\u043d\u043e, \u043d\u043e\u00a0\u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 <a href=\"http:\/\/Dispatchers.IO\" rel=\"noopener noreferrer nofollow\"><code>Dispatchers.IO<\/code><\/a> \u2192 <code>flow<\/code> \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u2192 <code>Dispatchers.IO<\/code>\u2026 \u0442\u044b\u0441\u044f\u0447\u0443 \u0440\u0430\u0437. \u0412\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a:<\/p>\n<ol>\n<li>\n<p>\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043d\u0430\u00a0\u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u043e\u043d \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d;<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 <code>withContext<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u043c\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0438 \u043f\u0440\u043e\u0441\u0430\u0434\u043a\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438\u00a0\u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u00a0\u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u00a0\u0434\u0440\u0443\u0433\u0438\u043c\u0438 suspend\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0430\u043c\u043e\u0435 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0435: \u044d\u0442\u043e <em>\u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441<\/em>, \u043d\u043e\u00a0\u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c <code>emit()<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442\u0441\u044f <strong>\u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u0442\u043e\u043a\u0430<\/strong>. \u0418\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0435\u0442 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f: <code>emit<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <strong>\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0447\u0442\u043e\u00a0\u0438 \u0437\u0430\u043f\u0443\u0441\u043a <\/strong><code><strong>collect<\/strong><\/code>, \u0435\u0441\u043b\u0438 <code>flowOn<\/code> \u043d\u0435\u00a0\u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0441\u044f. \u0410 <code>withContext<\/code> \u043b\u043e\u043c\u0430\u0435\u0442 \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u00a0\u2014 \u0438 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 <code>IllegalStateException: Flow invariant is violated<\/code>.<\/p>\n<p>\u0415\u0449\u0451 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"kotlin\">flow {     val data = withContext(Dispatchers.IO) {         heavyQuery()     }     emit(data) }<\/code><\/pre>\n<p>\u041d\u0430\u00a0\u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 <code>collect<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0434\u0440\u0443\u0433\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 <code>Main<\/code>), <code>emit(data)<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 <strong>\u043d\u0435\u00a0\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435<\/strong>, \u0433\u0434\u0435 <code>collect<\/code>, \u0438 Kotlin Flow \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p><code>withContext<\/code>\u00a0\u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043d\u0435\u00a0\u0434\u043b\u044f <code>Flow<\/code>. \u041e\u043d \u0445\u043e\u0440\u043e\u0448 \u0432\u00a0\u0442\u043e\u0447\u043a\u0430\u0445 \u0432\u0445\u043e\u0434\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043d\u0443\u0442\u0440\u0438 ViewModel), \u043d\u043e <em>\u0432\u043d\u0443\u0442\u0440\u0438 \u0446\u0435\u043f\u043e\u0447\u043a\u0438<\/em> \u0435\u0433\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0430 <code>flowOn<\/code>. \u0422\u0430\u043a \u0432\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442\u0435\u00a0\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430\u00a0\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0438 \u043d\u0435\u00a0\u0440\u0438\u0441\u043a\u0443\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u0430\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u00a0\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0438\u043b\u0438\u00a0\u0432\u00a0\u0431\u043e\u044e.<\/p>\n<h3>\u041c\u0438\u043d\u0438-\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/h3>\n<p>\u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u043c\u0435\u0442\u0440\u0438\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0447\u0443\u0432\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">suspend fun benchmark() {     val flowOnTime = measureTimeMillis {         (1..1_000_000).asFlow()             .map { it + 1 }                        \/\/ CPU             .flowOn(Dispatchers.Default)             .collect()     }      val withContextTime = measureTimeMillis {         (1..1_000_000).asFlow()             .map { withContext(Dispatchers.Default) { it + 1 } }             .collect()     }      println(\"flowOn:       $flowOnTime ms\")     println(\"withContext:  $withContextTime ms\") }<\/code><\/pre>\n<pre><code class=\"yaml\">flowOn:       150\u2013250 ms withContext:  3000\u20136000 ms<\/code><\/pre>\n<p>\u0412\u00a0\u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u0430\u00a0\u0434\u0435\u0441\u043a\u0442\u043e\u043f\u0435 flowOn\u2011\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u00a0\u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0434\u0435\u0440\u0436\u0438\u0442 CPU\u2011\u0447\u0430\u0441\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043f\u043e\u0447\u043a\u043e\u0439. <\/p>\n<p><code>flowOn(Dispatchers.Default)<\/code> \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0430\u043f\u0441\u0442\u0440\u0438\u043c \u043d\u0430\u00a0\u043d\u0443\u0436\u043d\u044b\u0439 \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440, \u0438 \u0432\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <em>\u043f\u043e\u0442\u043e\u0447\u043d\u043e<\/em>, \u0431\u0435\u0437\u00a0\u043b\u0438\u0448\u043d\u0438\u0445 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u0442\u0440\u0435\u0434\u0430\u043c\u0438.<\/p>\n<p><code>withContext(Dispatchers.Default)<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>map<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 <strong>\u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0442\u0440\u0435\u0434\u2011\u0441\u0432\u0438\u0442\u0447\u0435\u0439<\/strong>, \u043f\u043e\u00a0\u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u041a\u0430\u0436\u0434\u044b\u0439 <code>withContext<\/code>\u00a0\u2014 \u044d\u0442\u043e suspend, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0432\u043e\u0437\u0432\u0440\u0430\u0442, \u0438 \u0432\u0441\u0451 \u044d\u0442\u043e \u0432\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0435. \u0421\u0443\u043c\u043c\u0430\u0440\u043d\u044b\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043a\u043e\u043b\u043e\u0441\u0441\u0430\u043b\u044c\u043d\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u043a\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d (<code>-Dkotlinx.coroutines.debug<\/code> \u0438\u043b\u0438 <code>setprop debug.coroutines.enable_creation_stack_trace true<\/code>), \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u00a0\u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0438 \u043a\u0430\u043a\u00a0\u043e\u043d\u0438 \u0434\u0430\u0432\u044f\u0442 \u043d\u0430\u00a0\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a.<\/p>\n<hr\/>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p><code>flowOn<\/code> \u0438 <code>withContext<\/code> \u0440\u0435\u0448\u0430\u044e\u0442 \u0441\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043d\u043e\u00a0\u0434\u0435\u043b\u0430\u044e\u0442 \u044d\u0442\u043e <strong>\u043a\u0430\u0440\u0434\u0438\u043d\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u0443\u0442\u044f\u043c\u0438<\/strong>. <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u0430 \u0441\u00a0\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u0430\u0442\u0440\u0430\u0442\u0430\u043c\u0438; <code>withContext<\/code>\u00a0\u2014 \u0442\u043e\u0447\u0435\u0447\u043d\u043e\u0435, \u0438\u043d\u043e\u0433\u0434\u0430 \u0434\u043e\u0440\u043e\u0433\u043e\u0435 \u0438 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0435. \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u00a0\u0433\u043e\u043b\u043e\u0432\u0435 \u044d\u0442\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0438 \u043d\u0435\u00a0\u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u00a0backpressure, \u0432\u0430\u0448\u0438 Flows \u0431\u0443\u0434\u0443\u0442\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u043c\u0438 \u0438 \u0430\u0434\u0435\u043a\u0432\u0442\u0430\u043d\u044b\u043c\u0438.<\/p>\n<blockquote>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441\u00a0Java \u0438 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043e\u00a0\u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043d\u0430\u00a0Kotlin \u0434\u043b\u044f\u00a0\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430 \u043a\u0443\u0440\u0441\u0430 <strong>Kotlin Backend Developer. Professional<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong><em>12\u00a0\u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/em><\/strong>\u00a0\u2014 <a href=\"https:\/\/otus.pw\/7xcU\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044f\u0442 \u043d\u0430\u00a0Kotlin? \u0421\u0435\u043a\u0440\u0435\u0442\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0441\u00a0Java \u0434\u043b\u044f\u00a0\u0431\u044d\u043a\u0435\u043d\u0434\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432\u00bb<\/a>. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a\u00a0\u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430\u00a0Kotlin \u0432\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d\u0435.<\/p>\n<\/li>\n<li>\n<p><strong><em>19\u00a0\u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/em><\/strong>\u00a0\u2014 <a href=\"https:\/\/otus.pw\/Mn0S\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f Kotlin 1.9\u20132.2\u00a0\u0434\u043b\u044f\u00a0JVM\u00bb<\/a>. \u041e\u0431\u0441\u0443\u0434\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 \u0438 \u0438\u0445 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043d\u0430\u00a0\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435<a href=\"https:\/\/otus.pw\/UZWN\/\" rel=\"noopener noreferrer nofollow\"> \u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/a>, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438 \u0443\u0437\u043d\u0430\u0442\u044c, \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442\u00a0\u043b\u0438 \u0432\u0430\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abKotlin Backend Developer. Professional\u00bb.<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/932230\/\"> https:\/\/habr.com\/ru\/articles\/932230\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a <strong>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0432\u00a0Kotlin Flow<\/strong> \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043d\u0435\u00a0\u0442\u043e\u00a0\u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e <code>withContext<\/code>. <\/p>\n<h4>\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b<\/h4>\n<p>\u0412\u00a0Kotlin\u2011\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u0430\u043c\u0438 \u043f\u043e\u00a0\u0441\u0435\u0431\u0435 \u043d\u0435\u0434\u043e\u0440\u043e\u0433\u0438\u0435\u00a0\u2014 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u0442\u044b\u0441\u044f\u0447. \u041d\u043e\u00a0\u0441\u0430\u043c\u0430 \u043f\u043e\u00a0\u0441\u0435\u0431\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043d\u0435\u00a0\u0434\u0430\u0451\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439, \u0447\u0442\u043e\u00a0\u043e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u00a0\u043d\u0443\u0436\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435. \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 (\u0442\u043e \u0435\u0441\u0442\u044c <code>CoroutineDispatcher<\/code>)\u00a0\u2014 \u044d\u0442\u043e <em>\u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439<\/em> \u0432\u044b\u0431\u043e\u0440, \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u0442\u0440\u0435\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u043a\u043e\u0434. \u0418 \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 <code>flow<\/code> \u0438\u0437\u00a0UI, \u043d\u043e\u00a0\u043d\u0435\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 <code>Dispatchers.IO<\/code>, \u0432\u0435\u0441\u044c \u043a\u043e\u0434, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a\u00a0\u0431\u0430\u0437\u0435 \u0438\u043b\u0438\u00a0\u0444\u0430\u0439\u043b\u043e\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443, \u043f\u043e\u0435\u0434\u0435\u0442 \u0432\u00a0\u0433\u043b\u0430\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. <\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442\u00a0\u2014 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u043e\u0442\u043e\u043a\u0430. Kotlin Flow \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430\u00a0\u0442\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0451 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u043b\u043e\u0441\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u0432\u00a0\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435. \u0415\u0441\u043b\u0438 \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u0432\u043e\u0442\u043a\u043d\u0443\u0442\u044c <code>withContext<\/code> \u0438\u043b\u0438 <code>emit()<\/code> \u0438\u0437\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440\u0430\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 <code>IllegalStateException: Flow invariant is violated<\/code>. \u042d\u0442\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u0430\u043a\u00a0\u0440\u0430\u0437 \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442: \u00ab\u0442\u044b \u043d\u0430\u0440\u0443\u0448\u0438\u043b \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u00a0\u2014 emit \u043f\u043e\u0448\u0451\u043b \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430\u00bb. \u0420\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c\u00a0\u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>flowOn<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0431\u0443\u0444\u0435\u0440 \u0438 \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044f\u0435\u0442 upstream \u043e\u0442\u00a0downstream.<\/p>\n<p>\u041d\u0443 \u0438 \u0442\u0440\u0435\u0442\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442\u00a0\u2014 \u0433\u043e\u043d\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u0432\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u044d\u043c\u0438\u0442\u0430 \u0432\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0438\u0445 \u0432\u00a0\u043e\u0431\u0449\u0438\u0439 <code>StateFlow<\/code>, \u043f\u0440\u0438\u00a0\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0439\u043c\u0430\u0442\u044c \u0433\u043e\u043d\u043a\u0443: \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u041d\u0430\u00a0\u0442\u0435\u0441\u0442\u0430\u0445 \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e, \u0430\u00a0\u0432\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0440\u0435\u0434\u043a\u0438\u0439 \u0431\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0443\u0434\u043d\u043e \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0443\u0436\u043d\u043e \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e, \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0438 \u0447\u0435\u0440\u0435\u0437 <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439, \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431.<\/p>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 flowOn<\/h3>\n<p><code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 <strong>\u0433\u0434\u0435 \u0438 \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0430\u043f\u0441\u0442\u0440\u0438\u043c<\/strong>. <em>\u0410\u043f\u0441\u0442\u0440\u0438\u043c<\/em>\u00a0\u2014 \u044d\u0442\u043e \u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0438\u0434\u0451\u0442 <strong>\u0432\u044b\u0448\u0435<\/strong> \u043f\u043e\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u043f\u043e\u0441\u043b\u0435 <code>flow {}<\/code> \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0433\u043e \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0435\u0433\u043e <code>Flow<\/code>. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 <code>flowOn(Dispatchers.IO)<\/code>, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442: \u00ab\u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0432\u044b\u0448\u0435\u00a0\u2014 <code>emit<\/code>, <code>map<\/code>, <code>filter<\/code> \u0438 \u043f\u0440\u043e\u0447\u0435\u0435\u00a0\u2014 \u043f\u0443\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 <code>Dispatchers.IO<\/code>\u00bb. \u0410\u00a0\u0432\u043e\u0442 \u0432\u0441\u0451, \u0447\u0442\u043e <strong>\u043d\u0438\u0436\u0435<\/strong>, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435 <code>flowOn<\/code>, \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u043e\u0440.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"kotlin\">val usersFlow = flow {     logThread(\"start emit\")           emit(loadUsers())             }.flowOn(Dispatchers.IO)           .map { user -&gt; enrich(user) }   <\/code><\/pre>\n<p><em>\u0427\u0442\u043e\u00a0\u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0451\u0442:<\/em><\/p>\n<ul>\n<li>\n<p><code>loadUsers()<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d <strong>\u043d\u0430\u00a0IO<\/strong><\/p>\n<\/li>\n<li>\n<p><code>map { enrich(...) }<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0443\u0436\u0435 <strong>\u0432\u00a0\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u0432\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u0438\u0442\u0435 flow<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>Main<\/code> \u0438\u043b\u0438 <code>Default<\/code>)<\/p>\n<\/li>\n<\/ul>\n<p>\u042d\u0442\u0430 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u00a0\u2014 <em>\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u043e\u043c \u0438 \u0434\u0430\u0443\u043d\u0441\u0442\u0440\u0438\u043c\u043e\u043c<\/em>\u00a0\u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0430\u0432\u043a\u0443 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430. \u0418\u043c\u0435\u043d\u043d\u043e <code>flowOn<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0430\u0443\u0437\u0443 \u043c\u0435\u0436\u0434\u0443 \u0447\u0430\u0441\u0442\u044f\u043c\u0438 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0438 \u043d\u0435\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u0443 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0443\u043d\u0441\u0442\u0440\u0438\u043c. \u0415\u0441\u043b\u0438 <code>loadUsers()<\/code> \u0432\u0434\u0440\u0443\u0433 \u043d\u0430\u0447\u043d\u0451\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c, \u044d\u0442\u043e \u043d\u0435\u00a0\u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 <code>collect<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0435\u0441\u0442\u044c \u0431\u0443\u0444\u0435\u0440. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>flowOn<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e \u0441\u00a0\u0442\u044f\u0436\u0451\u043b\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e <code>flowOn<\/code> \u0432\u043b\u0438\u044f\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0432\u044b\u0448\u0435 \u0441\u0435\u0431\u044f<\/strong>. \u041e\u043d \u043d\u0435\u00a0\u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u0444\u043b\u043e\u0443 \u0446\u0435\u043b\u0438\u043a\u043e\u043c. \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"kotlin\">flow {     emit(doSomething()) }.map { heavyCompute(it) }  .flowOn(Dispatchers.Default)<\/code><\/pre>\n<p><code>heavyCompute<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f <strong>\u043d\u0435<\/strong> \u043d\u0430 <code>Dispatchers.Default<\/code>, \u0430\u00a0\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0433\u0434\u0435 \u0438\u0434\u0451\u0442 <code>collect<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>flowOn<\/code> \u0432\u043b\u0438\u044f\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 <\/strong><code><strong>emit<\/strong><\/code><strong> \u0438 \u0432\u0441\u0451 \u0432\u044b\u0448\u0435<\/strong>. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0438 <code>emit<\/code>, \u0438 <code>map<\/code>\u00a0\u0431\u044b\u043b\u0438 \u043d\u0430 <code>Default<\/code>, \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>flowOn<\/code> \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"kotlin\">flow {     emit(doSomething()) }     .flowOn(Dispatchers.IO)     .map { heavyCompute(it) }     .flowOn(Dispatchers.Default)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>flowOn<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u00a0\u2014 \u0438 \u044d\u0442\u043e \u043a\u0430\u043a\u00a0\u0440\u0430\u0437 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0433\u0438\u0431\u043a\u0438\u0435 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u044b, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u043c \u043f\u0443\u043b\u0435. \u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0442\u043e, \u0447\u0442\u043e\u00a0\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f <code>flowOn<\/code> \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437\u00a0\u043d\u0438\u0445 \u0432\u043b\u0438\u044f\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 <strong>\u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0432\u044b\u0448\u0435<\/strong>.<\/p>\n<p><code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e <strong>\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 <\/strong><code><strong>Flow<\/strong><\/code>, \u0431\u0435\u0437\u00a0\u0440\u0438\u0441\u043a\u0430 \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043b\u0438\u00a0\u043d\u0430\u0440\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>IllegalStateException<\/code>. <\/p>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 withContext (\u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u043d\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)<\/h3>\n<p><code>withContext<\/code>\u00a0\u2014 \u044d\u0442\u043e <strong>suspend\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u044f<\/strong>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0432\u00a0\u0434\u0440\u0443\u0433\u043e\u0439 <code>CoroutineContext<\/code>. \u041e\u043d\u0430 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f\u00a0\u0437\u0430\u0434\u0430\u0447 \u0432\u0440\u043e\u0434\u0435 <code>withContext(Dispatchers.IO) { readFromDisk() }<\/code>, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u0443\u044e \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432\u00a0\u043d\u0443\u0436\u043d\u043e\u043c \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440\u0435. \u041d\u043e\u00a0\u0432\u043d\u0443\u0442\u0440\u0438 <code>Flow<\/code> \u0432\u0441\u0451 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u00a0\u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 <code>withContext<\/code> <strong>\u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0435\u043b\u0430 <\/strong><code><strong>flow {}<\/strong><\/code><strong> \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0438\u043f\u0430 <\/strong><code><strong>map<\/strong><\/code>, \u0432\u044b \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0435\u043d\u044f\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u0412\u044b <strong>\u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0435 <\/strong><code><strong>suspend<\/strong><\/code><strong>\u2011\u0442\u043e\u0447\u043a\u0443 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442<\/strong> \u0438 <strong>\u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435 \u0442\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437<\/strong>, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0435\u0441\u043b\u0438 \u0443\u00a0\u0432\u0430\u0441 \u0442\u044b\u0441\u044f\u0447\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\u00a0\u2014 \u0432\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0442\u044b\u0441\u044f\u0447\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u0442\u0440\u0435\u0434\u043e\u0432. \u0410\u00a0\u044d\u0442\u043e \u0443\u0436\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430.<\/p>\n<pre><code class=\"kotlin\">val wrongFlow = flow {     (1..1000).forEach { id -&gt;         emit(withContext(Dispatchers.IO) {             loadById(id)         })     } }<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u0432\u0438\u043d\u043d\u043e, \u043d\u043e\u00a0\u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 <a href=\"http:\/\/Dispatchers.IO\" rel=\"noopener noreferrer nofollow\"><code>Dispatchers.IO<\/code><\/a> \u2192 <code>flow<\/code> \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u2192 <code>Dispatchers.IO<\/code>\u2026 \u0442\u044b\u0441\u044f\u0447\u0443 \u0440\u0430\u0437. \u0412\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a:<\/p>\n<ol>\n<li>\n<p>\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043d\u0430\u00a0\u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u043e\u043d \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d;<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 <code>withContext<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u043c\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0438 \u043f\u0440\u043e\u0441\u0430\u0434\u043a\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438\u00a0\u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u00a0\u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u00a0\u0434\u0440\u0443\u0433\u0438\u043c\u0438 suspend\u2011\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0430\u043c\u043e\u0435 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0435: \u044d\u0442\u043e <em>\u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441<\/em>, \u043d\u043e\u00a0\u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c <code>emit()<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442\u0441\u044f <strong>\u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u0442\u043e\u043a\u0430<\/strong>. \u0418\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0435\u0442 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f: <code>emit<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <strong>\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0447\u0442\u043e\u00a0\u0438 \u0437\u0430\u043f\u0443\u0441\u043a <\/strong><code><strong>collect<\/strong><\/code>, \u0435\u0441\u043b\u0438 <code>flowOn<\/code> \u043d\u0435\u00a0\u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0441\u044f. \u0410 <code>withContext<\/code> \u043b\u043e\u043c\u0430\u0435\u0442 \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u00a0\u2014 \u0438 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 <code>IllegalStateException: Flow invariant is violated<\/code>.<\/p>\n<p>\u0415\u0449\u0451 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"kotlin\">flow {     val data = withContext(Dispatchers.IO) {         heavyQuery()     }     emit(data) }<\/code><\/pre>\n<p>\u041d\u0430\u00a0\u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 <code>collect<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0434\u0440\u0443\u0433\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 <code>Main<\/code>), <code>emit(data)<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 <strong>\u043d\u0435\u00a0\u0432\u00a0\u0442\u043e\u043c\u00a0\u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435<\/strong>, \u0433\u0434\u0435 <code>collect<\/code>, \u0438 Kotlin Flow \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p><code>withContext<\/code>\u00a0\u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043d\u0435\u00a0\u0434\u043b\u044f <code>Flow<\/code>. \u041e\u043d \u0445\u043e\u0440\u043e\u0448 \u0432\u00a0\u0442\u043e\u0447\u043a\u0430\u0445 \u0432\u0445\u043e\u0434\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043d\u0443\u0442\u0440\u0438 ViewModel), \u043d\u043e <em>\u0432\u043d\u0443\u0442\u0440\u0438 \u0446\u0435\u043f\u043e\u0447\u043a\u0438<\/em> \u0435\u0433\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0430 <code>flowOn<\/code>. \u0422\u0430\u043a \u0432\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442\u0435\u00a0\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430\u00a0\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0438 \u043d\u0435\u00a0\u0440\u0438\u0441\u043a\u0443\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u0430\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u00a0\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0438\u043b\u0438\u00a0\u0432\u00a0\u0431\u043e\u044e.<\/p>\n<h3>\u041c\u0438\u043d\u0438-\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/h3>\n<p>\u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u043c\u0435\u0442\u0440\u0438\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0447\u0443\u0432\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">suspend fun benchmark() {     val flowOnTime = measureTimeMillis {         (1..1_000_000).asFlow()             .map { it + 1 }                        \/\/ CPU             .flowOn(Dispatchers.Default)             .collect()     }      val withContextTime = measureTimeMillis {         (1..1_000_000).asFlow()             .map { withContext(Dispatchers.Default) { it + 1 } }             .collect()     }      println(\"flowOn:       $flowOnTime ms\")     println(\"withContext:  $withContextTime ms\") }<\/code><\/pre>\n<pre><code class=\"yaml\">flowOn:       150\u2013250 ms withContext:  3000\u20136000 ms<\/code><\/pre>\n<p>\u0412\u00a0\u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u0430\u00a0\u0434\u0435\u0441\u043a\u0442\u043e\u043f\u0435 flowOn\u2011\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u00a0\u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0434\u0435\u0440\u0436\u0438\u0442 CPU\u2011\u0447\u0430\u0441\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043f\u043e\u0447\u043a\u043e\u0439. <\/p>\n<p><code>flowOn(Dispatchers.Default)<\/code> \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0430\u043f\u0441\u0442\u0440\u0438\u043c \u043d\u0430\u00a0\u043d\u0443\u0436\u043d\u044b\u0439 \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0435\u0440, \u0438 \u0432\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <em>\u043f\u043e\u0442\u043e\u0447\u043d\u043e<\/em>, \u0431\u0435\u0437\u00a0\u043b\u0438\u0448\u043d\u0438\u0445 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u0442\u0440\u0435\u0434\u0430\u043c\u0438.<\/p>\n<p><code>withContext(Dispatchers.Default)<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>map<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 <strong>\u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0442\u0440\u0435\u0434\u2011\u0441\u0432\u0438\u0442\u0447\u0435\u0439<\/strong>, \u043f\u043e\u00a0\u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u041a\u0430\u0436\u0434\u044b\u0439 <code>withContext<\/code>\u00a0\u2014 \u044d\u0442\u043e suspend, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0432\u043e\u0437\u0432\u0440\u0430\u0442, \u0438 \u0432\u0441\u0451 \u044d\u0442\u043e \u0432\u00a0\u0446\u0435\u043f\u043e\u0447\u043a\u0435. \u0421\u0443\u043c\u043c\u0430\u0440\u043d\u044b\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043a\u043e\u043b\u043e\u0441\u0441\u0430\u043b\u044c\u043d\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u043a\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d (<code>-Dkotlinx.coroutines.debug<\/code> \u0438\u043b\u0438 <code>setprop debug.coroutines.enable_creation_stack_trace true<\/code>), \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u00a0\u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0438 \u043a\u0430\u043a\u00a0\u043e\u043d\u0438 \u0434\u0430\u0432\u044f\u0442 \u043d\u0430\u00a0\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a.<\/p>\n<hr\/>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p><code>flowOn<\/code> \u0438 <code>withContext<\/code> \u0440\u0435\u0448\u0430\u044e\u0442 \u0441\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043d\u043e\u00a0\u0434\u0435\u043b\u0430\u044e\u0442 \u044d\u0442\u043e <strong>\u043a\u0430\u0440\u0434\u0438\u043d\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u0443\u0442\u044f\u043c\u0438<\/strong>. <code>flowOn<\/code>\u00a0\u2014 \u044d\u0442\u043e \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0430\u043f\u0441\u0442\u0440\u0438\u043c\u0430 \u0441\u00a0\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u0430\u0442\u0440\u0430\u0442\u0430\u043c\u0438; <code>withContext<\/code>\u00a0\u2014 \u0442\u043e\u0447\u0435\u0447\u043d\u043e\u0435, \u0438\u043d\u043e\u0433\u0434\u0430 \u0434\u043e\u0440\u043e\u0433\u043e\u0435 \u0438 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0435. \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u00a0\u0433\u043e\u043b\u043e\u0432\u0435 \u044d\u0442\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0438 \u043d\u0435\u00a0\u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u00a0backpressure, \u0432\u0430\u0448\u0438 Flows \u0431\u0443\u0434\u0443\u0442\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u043c\u0438 \u0438 \u0430\u0434\u0435\u043a\u0432\u0442\u0430\u043d\u044b\u043c\u0438.<\/p>\n<blockquote>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441\u00a0Java \u0438 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043e\u00a0\u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043d\u0430\u00a0Kotlin \u0434\u043b\u044f\u00a0\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430 \u043a\u0443\u0440\u0441\u0430 <strong>Kotlin Backend Developer. Professional<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong><em>12\u00a0\u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/em><\/strong>\u00a0\u2014 <a href=\"https:\/\/otus.pw\/7xcU\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044f\u0442 \u043d\u0430\u00a0Kotlin? \u0421\u0435\u043a\u0440\u0435\u0442\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0441\u00a0Java \u0434\u043b\u044f\u00a0\u0431\u044d\u043a\u0435\u043d\u0434\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432\u00bb<\/a>. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a\u00a0\u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430\u00a0Kotlin \u0432\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d\u0435.<\/p>\n<\/li>\n<li>\n<p><strong><em>19\u00a0\u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/em><\/strong>\u00a0\u2014 <a href=\"https:\/\/otus.pw\/Mn0S\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f Kotlin 1.9\u20132.2\u00a0\u0434\u043b\u044f\u00a0JVM\u00bb<\/a>. \u041e\u0431\u0441\u0443\u0434\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 \u0438 \u0438\u0445 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043d\u0430\u00a0\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435<a href=\"https:\/\/otus.pw\/UZWN\/\" rel=\"noopener noreferrer nofollow\"> \u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/a>, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438 \u0443\u0437\u043d\u0430\u0442\u044c, \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442\u00a0\u043b\u0438 \u0432\u0430\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abKotlin Backend Developer. Professional\u00bb.<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/932230\/\"> https:\/\/habr.com\/ru\/articles\/932230\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-473165","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=473165"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473165\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=473165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=473165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=473165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}