{"id":1553,"date":"2022-06-03T10:18:58","date_gmt":"2022-06-03T10:18:58","guid":{"rendered":"http:\/\/sophyonline.com\/blog\/?p=1553"},"modified":"2022-06-03T10:18:58","modified_gmt":"2022-06-03T10:18:58","slug":"actor-isolated-property-can-not-be-referenced-from-a-non-isolated-context","status":"publish","type":"post","link":"http:\/\/sophyonline.com\/blog\/2022\/06\/actor-isolated-property-can-not-be-referenced-from-a-non-isolated-context\/","title":{"rendered":"<code>Actor-isolated property can not be referenced from a non-isolated context<\/code>"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I was in need some days back to create a mock that conformed to an Actor protocol. And when trying to accessing one of their properties I couldn&#8217;t do it. And the error that you see on the title of this post was showed. This was an obvious thing that I found out after reading the Actor text again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When using Actors, you need to understand that they will provide you with a mechanism to isolate their state from the rest of the program. If we read the documentation:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>An actor is a reference type that protects access to its mutable state. They protect their state from data races.<\/p><p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Actor isolation<\/mark><\/strong> is how actors protect their mutable state. For actors, the primary mechanism for this protection is by only allowing their stored instance properties to be accessed directly on\u00a0<code>self<\/code><\/p><p><\/p><cite>https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0306-actors.md<\/cite><\/blockquote>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I was trying then to access a property of my mock that was in the end an Actor type like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: swift; title: ; notranslate\" title=\"\">\nmock.deleteCallsCount\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">\u00a0This is invalid, and the only way to access this property is through a function inside the Actor class that will then access the property value. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: as3; title: ; notranslate\" title=\"\">\npublic actor MyMock: SomeProtocol  {\n    \n     var deleteCallsCount = 0\n     var deleteCalled = false\n\n    public func deleteCallsCount() -&gt; Int {\n        deleteCallsCount\n    }\n    \n    public func deleteCalled() -&gt; Bool {\n        deleteCalled\n    }\n}\n\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\" id=\"block-2e145915-5b1d-4e21-a0c0-84f726aa99fc\">And finally, we need to use the await before the call of the method<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: swift; title: ; notranslate\" title=\"\">\nlet resultCallsCount = await myMock.deleteCallsCount()\n\nlet resultCalled = await myMock.deleteCalled()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\" id=\"block-2e145915-5b1d-4e21-a0c0-84f726aa99fc\">In this way we can access our Actor property avoiding data races and the invalid message from the compiler.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was in need some days back to create a mock that conformed to an Actor protocol. And when trying to accessing one of their properties I couldn&#8217;t do it. And the error that you see on the title of this post was showed. This was an obvious thing that I found out after reading <a class=\"read-more\" href=\"http:\/\/sophyonline.com\/blog\/2022\/06\/actor-isolated-property-can-not-be-referenced-from-a-non-isolated-context\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[1],"tags":[48,50,20],"class_list":["post-1553","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-actors-swift","tag-await","tag-swift"],"_links":{"self":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/comments?post=1553"}],"version-history":[{"count":11,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1553\/revisions"}],"predecessor-version":[{"id":1564,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1553\/revisions\/1564"}],"wp:attachment":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/media?parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/categories?post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/tags?post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}