@@ -2,28 +2,29 @@ import Foundation
22import os. log
33import XPCShared
44
5- var asyncService : AsyncXPCService ?
6- var shared = XPCService ( )
5+ let shared = XPCService ( )
76
87public func getService( ) throws -> AsyncXPCService {
98 if ProcessInfo . processInfo. environment [ " XCODE_RUNNING_FOR_PREVIEWS " ] == " 1 " {
109 struct RunningInPreview : Error { }
1110 throw RunningInPreview ( )
1211 }
13- if shared. isInvalidated {
14- shared = XPCService ( )
15- asyncService = nil
16- }
17- if let asyncService { return asyncService }
18- let service = AsyncXPCService ( connection: shared. connection)
19- asyncService = service
20- return service
12+ return AsyncXPCService ( service: shared)
2113}
2214
2315class XPCService {
24- var isInvalidated = false
25-
26- lazy var connection : NSXPCConnection = {
16+ private var isInvalidated = false
17+ private lazy var _connection : NSXPCConnection = buildConnection ( )
18+
19+ var connection : NSXPCConnection {
20+ if isInvalidated {
21+ isInvalidated = false
22+ rebuildConnection ( )
23+ }
24+ return _connection
25+ }
26+
27+ private func buildConnection( ) -> NSXPCConnection {
2728 let connection = NSXPCConnection (
2829 machServiceName: Bundle ( for: XPCService . self)
2930 . object ( forInfoDictionaryKey: " BUNDLE_IDENTIFIER_BASE " ) as! String + " .XPCService "
@@ -36,12 +37,17 @@ class XPCService {
3637 }
3738 connection. interruptionHandler = { [ weak self] in
3839 os_log ( . info, " XPCService interrupted " )
40+ self ? . isInvalidated = true
3941 }
4042 connection. resume ( )
4143 return connection
42- } ( )
44+ }
45+
46+ func rebuildConnection( ) {
47+ _connection = buildConnection ( )
48+ }
4349
4450 deinit {
45- connection . invalidate ( )
51+ _connection . invalidate ( )
4652 }
4753}
0 commit comments