Skip to content

Commit

Permalink
Delete NSXPCConnection#invalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgto committed Apr 15, 2024
1 parent ac220d4 commit 165488b
Showing 1 changed file with 16 additions and 46 deletions.
62 changes: 16 additions & 46 deletions example-nsxpc-throws-error/ContentView.swift
Expand Up @@ -4,6 +4,7 @@
import SwiftUI

struct ContentView: View {
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
@State var nothingResult: String = "Waiting…"
@State var callbackResult: String = "Waiting…"
@State var simpleResult: String = "Waiting…"
Expand All @@ -17,13 +18,7 @@ struct ContentView: View {
Section("Run XPC Call with no argument and no return value") {
Button("Run…") {
nothingResult = "Running…"
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
proxy.performNothing()
nothingResult = "Done"
}
Expand All @@ -32,13 +27,7 @@ struct ContentView: View {
Section("Run XPC Call with no argument and no return value using callback") {
Button("Run…") {
callbackResult = "Running…"
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
proxy.performCallback {
callbackResult = "Done"
}
Expand All @@ -48,13 +37,7 @@ struct ContentView: View {
Section("Run XPC Call with two Int arguments and returns Int by callback closure") {
Button("Run…") {
simpleResult = "Running…"
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
proxy.performCalculation(firstNumber: 100, secondNumber: 200) { sum in
simpleResult = "SUM: \(sum)"
}
Expand All @@ -65,13 +48,7 @@ struct ContentView: View {
Button("Run…") {
simpleAsyncNothingResult = "Running…"
Task {
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
await proxy.performNothingAsync()
simpleAsyncNothingResult = "Done"
}
Expand All @@ -82,13 +59,7 @@ struct ContentView: View {
Button("Run…") {
simpleAsyncResult = "Running…"
Task {
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
let sum = await proxy.performCalculationAsync(firstNumber: 100, secondNumber: 200)
simpleAsyncResult = "SUM: \(sum)"
}
Expand All @@ -99,13 +70,7 @@ struct ContentView: View {
Button("Run…") {
errorResult = "Running…"
Task {
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
defer {
service.invalidate()
}
guard let proxy = proxy() else { return }
do {
try await proxy.performThrowsError()
} catch {
Expand All @@ -126,10 +91,7 @@ struct ContentView: View {
Button("Run…") {
nserrorResult = "Running…"
Task {
let service = NSXPCConnection(serviceName: "net.mtgto.example-nsxpc-throws-error.ExampleXpc")
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
guard let proxy = service.remoteObjectProxy as? any ExampleXpcProtocol else { return }
guard let proxy = proxy() else { return }
defer {
service.invalidate()
}
Expand All @@ -150,6 +112,14 @@ struct ContentView: View {
.formStyle(.grouped)
.padding()
}

func proxy() -> ExampleXpcProtocol? {
service.remoteObjectInterface = NSXPCInterface(with: ExampleXpcProtocol.self)
service.activate()
return service.remoteObjectProxyWithErrorHandler { error in
print("Error: \(error)")
} as? any ExampleXpcProtocol
}
}

#Preview {
Expand Down

0 comments on commit 165488b

Please sign in to comment.