Skip to content

Commit 2f78364

Browse files
Merge pull request #590 from appwrite/fix-apple-xcode14-realtime
2 parents 8958ccf + 96f285d commit 2f78364

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

templates/swift/Sources/Services/Service.swift.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class {{ service.name | caseUcfirst }}: Service {
3434
{%~ if 'multipart/form-data' in method.consumes %}
3535
onProgress: ((UploadProgress) -> Void)? = nil
3636
{%~ endif %}
37-
) async throws -> {{ method | returnType(spec) | raw }} {
37+
){%~ if method.type != "webAuth" %} async{% endif %} throws -> {{ method | returnType(spec) | raw }} {
3838
{{~ include('swift/base/params.twig') }}
3939
{%~ if method.type == 'webAuth' %}
4040
{{~ include('swift/base/requests/OAuth.twig') }}

templates/swift/Sources/WebSockets/WebSocketClient.swift.twig

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public class WebSocketClient {
2626
var channel: Channel? = nil
2727
var tlsEnabled: Bool = false
2828
var closeSent: Bool = false
29-
30-
let upgradedSignalled = DispatchSemaphore(value: 0)
31-
let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE)
32-
let threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
29+
30+
let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE, qos: .background)
31+
32+
var threadGroup: MultiThreadedEventLoopGroup? = nil
3333

3434
weak var delegate: WebSocketClientDelegate? = nil
3535

@@ -184,6 +184,10 @@ public class WebSocketClient {
184184
self.maxFrameSize = maxFrameSize
185185
self.tlsEnabled = tlsEnabled
186186
self.delegate = delegate
187+
188+
DispatchQueue.global(qos: .background).async {
189+
self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
190+
}
187191
}
188192

189193
/// Create a new `WebSocketClient`.
@@ -207,10 +211,14 @@ public class WebSocketClient {
207211
self.maxFrameSize = 24
208212
self.tlsEnabled = tlsEnabled
209213
self.delegate = delegate
214+
215+
DispatchQueue.global(qos: .background).async {
216+
self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
217+
}
210218
}
211219

212220
deinit {
213-
try! threadGroup.syncShutdownGracefully()
221+
try! threadGroup!.syncShutdownGracefully()
214222
}
215223

216224
// MARK: - Open connection
@@ -221,16 +229,16 @@ public class WebSocketClient {
221229
SocketOptionLevel(SOL_SOCKET),
222230
SO_REUSEPORT
223231
)
232+
233+
while(threadGroup == nil) {}
224234

225-
let bootstrap = ClientBootstrap(group: threadGroup)
235+
let bootstrap = ClientBootstrap(group: threadGroup!)
226236
.channelOption(socketOptions, value: 1)
227237
.channelInitializer(self.openChannel)
228238

229239
_ = try bootstrap
230240
.connect(host: self.host, port: self.port)
231241
.wait()
232-
233-
self.upgradedSignalled.wait()
234242
}
235243

236244
private func openChannel(channel: Channel) -> EventLoopFuture<Void> {
@@ -260,13 +268,10 @@ public class WebSocketClient {
260268
}
261269

262270
private func upgradePipelineHandler(channel: Channel, response: HTTPResponseHead) -> EventLoopFuture<Void> {
263-
self.onOpen(channel)
264-
265271
let handler = MessageHandler(client: self)
266272

267273
if response.status == .switchingProtocols {
268274
self.channel = channel
269-
self.upgradedSignalled.signal()
270275
}
271276

272277
return channel.pipeline.addHandler(handler)

tests/AppleSwift55Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AppleSwift55Test extends Base
2626
...Base::LARGE_FILE_RESPONSES,
2727
...Base::LARGE_FILE_RESPONSES,
2828
...Base::EXCEPTION_RESPONSES,
29-
//...Base::REALTIME_RESPONSES,
29+
...Base::REALTIME_RESPONSES,
3030
...Base::COOKIE_RESPONSES,
3131
...Base::QUERY_HELPER_RESPONSES,
3232
...Base::PERMISSION_HELPER_RESPONSES,

tests/languages/apple/Tests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class Tests: XCTestCase {
3131
let realtime = Realtime(client)
3232
var realtimeResponse = "Realtime failed!"
3333

34-
// let expectation = XCTestExpectation(description: "realtime server")
35-
//
36-
// realtime.subscribe(channels: ["tests"]) { message in
37-
// realtimeResponse = message.payload!["response"] as! String
38-
// expectation.fulfill()
39-
// }
34+
let expectation = XCTestExpectation(description: "realtime server")
35+
36+
realtime.subscribe(channels: ["tests"]) { message in
37+
realtimeResponse = message.payload!["response"] as! String
38+
expectation.fulfill()
39+
}
4040

4141
var mock: Mock
4242

@@ -132,8 +132,8 @@ class Tests: XCTestCase {
132132
print(error.localizedDescription)
133133
}
134134

135-
// wait(for: [expectation], timeout: 10.0)
136-
// print(realtimeResponse)
135+
wait(for: [expectation], timeout: 10.0)
136+
print(realtimeResponse)
137137

138138
mock = try await general.setCookie()
139139
print(mock.result)

0 commit comments

Comments
 (0)