Skip to content

Commit b2d75c7

Browse files
committed
Recreate browser and advertiser between discovery sessions (Closes #12)
1 parent 750d3a4 commit b2d75c7

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Sources/MultipeerKit/Internal API/MultipeerConnection.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ final class MultipeerConnection: NSObject, MultipeerProtocol {
3838
func resume() {
3939
os_log("%{public}@", log: log, type: .debug, #function)
4040

41-
if modes.contains(.receiver) {
42-
advertiser.startAdvertisingPeer()
43-
}
4441
if modes.contains(.transmitter) {
42+
// Ideally, we'd just keep using the same browser for the lifetime of the MultipeerConnection object.
43+
// However, due to #12, we can't. The same process is done with the advertiser for consistency.
44+
browser = makeBrowser()
4545
browser.startBrowsingForPeers()
4646
}
47+
if modes.contains(.receiver) {
48+
advertiser = makeAdvertiser()
49+
advertiser.startAdvertisingPeer()
50+
}
4751
}
4852

4953
func stop() {
@@ -69,21 +73,25 @@ final class MultipeerConnection: NSObject, MultipeerProtocol {
6973
return s
7074
}()
7175

72-
private lazy var browser: MCNearbyServiceBrowser = {
76+
private func makeBrowser() -> MCNearbyServiceBrowser {
7377
let b = MCNearbyServiceBrowser(peer: me, serviceType: configuration.serviceType)
7478

7579
b.delegate = self
7680

7781
return b
78-
}()
82+
}
83+
84+
private lazy var browser: MCNearbyServiceBrowser = { makeBrowser() }()
7985

80-
private lazy var advertiser: MCNearbyServiceAdvertiser = {
86+
private func makeAdvertiser() -> MCNearbyServiceAdvertiser {
8187
let a = MCNearbyServiceAdvertiser(peer: me, discoveryInfo: nil, serviceType: configuration.serviceType)
8288

8389
a.delegate = self
8490

8591
return a
86-
}()
92+
}
93+
94+
private lazy var advertiser: MCNearbyServiceAdvertiser = { makeAdvertiser() }()
8795

8896
func broadcast(_ data: Data) throws {
8997
guard !session.connectedPeers.isEmpty else {

0 commit comments

Comments
 (0)