Created
June 27, 2025 00:29
-
-
Save cmsj/df19e7ff11d9eed00d4f9b30e08eeeb2 to your computer and use it in GitHub Desktop.
Not proud of this, but needs must.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git Worker/AppState.swift Worker/AppState.swift | |
index 06515fc..f09e3c7 100644 | |
--- Worker/AppState.swift | |
+++ Worker/AppState.swift | |
@@ -33,14 +33,14 @@ final class AppState { | |
// Approach 2: | |
// | |
// Trying to "un-own" the products array also doesn't work: | |
- let localProducts = self.products | |
+// let localProducts = self.products | |
// Now only localProducts references what was once self.products, right? | |
- self.products = [] | |
+// self.products = [] | |
// ERROR: Non-sendable result type '[WorkProduct]' cannot be sent from | |
// actor-isolated context in call to instance method 'processProducts' | |
- self.products = await worker.processProducts(localProducts) | |
+ self.products = await worker.processProducts(self.products.map { $0.asProxy() }) | |
return self.products.count | |
} | |
diff --git Worker/WorkProduct.swift Worker/WorkProduct.swift | |
index ac6c609..cfb65cc 100644 | |
--- Worker/WorkProduct.swift | |
+++ Worker/WorkProduct.swift | |
@@ -7,8 +7,28 @@ | |
import Foundation | |
+struct WorkProductProxy { | |
+ var id: UUID | |
+ var name: String? | |
+ var data: Data | |
+} | |
+ | |
nonisolated final class WorkProduct { | |
- var id = UUID() | |
- var name : String? = nil | |
- var data = Data(repeating: 0xFF, count: 1024) | |
+ var id: UUID | |
+ var name: String? | |
+ var data: Data | |
+ | |
+ init(id: UUID = UUID(), name: String? = nil, data: Data = Data(repeating: 0xFF, count: 1024)) { | |
+ self.id = id | |
+ self.name = name | |
+ self.data = data | |
+ } | |
+ | |
+ convenience init(_ proxy: WorkProductProxy) { | |
+ self.init(id: proxy.id, name: proxy.name, data: proxy.data) | |
+ } | |
+ | |
+ func asProxy() -> WorkProductProxy { | |
+ .init(id: id, name: name, data: data) | |
+ } | |
} | |
diff --git Worker/Worker.swift Worker/Worker.swift | |
index 56639c7..70abd71 100644 | |
--- Worker/Worker.swift | |
+++ Worker/Worker.swift | |
@@ -27,22 +27,21 @@ actor Worker { | |
return products | |
} | |
- func processProducts(_ products: sending [WorkProduct]) -> [WorkProduct] { | |
+ func processProducts(_ productProxies: consuming [WorkProductProxy]) -> sending [WorkProduct] { | |
+ let products: [WorkProduct] = productProxies.map { .init($0) } | |
+ | |
for product in products { | |
- processProduct(product) | |
+ // NOTE: You can't call processProduct() here anymore, seems like Swift just can't track the lifecycle that deeply? | |
+ if self.someSetting { | |
+ product.name = "abc" | |
+ } | |
+ else { | |
+ product.name = generateRandomString(length: 5) | |
+ } | |
} | |
return products | |
} | |
- | |
- func processProduct(_ product: WorkProduct) { | |
- if self.someSetting { | |
- product.name = "abc" | |
- } | |
- else { | |
- product.name = generateRandomString(length: 5) | |
- } | |
- } | |
} | |
fileprivate let characters = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment