gpt4 book ai didi

ios - MultiPeer MCNearbyBrowserService 崩溃

转载 作者:可可西里 更新时间:2023-11-01 04:44:01 26 4
gpt4 key购买 nike

我有一个使用 MultiPeer Connectivity 框架的应用程序。每次应用程序在 AppDelegate 中变得活跃时,我都会创建一个新的 MCSession 一个 MCNearbyBrowserService 和一个 MCNearbyAdvertiserService 并调用开始浏览和开始广告。然后,每次应用程序在 AppDelegate 中变为非事件状态时,我都会停止浏览和广告并将所有内容设置为 nil。我发现 MCNearbyBrowserService 导致其 syncQueue 崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -    [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[2]'
*** First throw call stack:
(0x2de3ee8b 0x381396c7 0x2dd7caef 0x2dd7c8b3 0x2f648167 0x2f6493af 0x3861e103 0x38622e77 0x3861ff9b 0x38623751 0x386239d1 0x3874ddff 0x3874dcc4)
libc++abi.dylib: terminating with uncaught exception of type NSException

有时在应用程序重新打开时。

这是我的 applicationDidBecomeActive 代码:

self.myIdentifier = [[MCPeerID alloc] initWithDisplayName:[self.class createHash:20]];

self.mainSession = [[MCSession alloc] initWithPeer:self.myIdentifier];
self.mainSession.delegate = self;

peerAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.myIdentifier discoveryInfo:nil serviceType: service];
peerAdvertiser.delegate = self;
peerBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.myIdentifier serviceType: service];
peerBrowser.delegate = self;

acceptReset = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reset) userInfo:nil repeats:YES];
acceptPosts = true;
[peerBrowser startBrowsingForPeers];
[peerAdvertiser startAdvertisingPeer];

self.isBrowsing = true;

这是我的 applicationWillResignActive 代码:

[acceptReset invalidate];
[peerAdvertiser stopAdvertisingPeer];
[peerBrowser stopBrowsingForPeers];
[self.mainSession disconnect];
self.mainSession = false;
self.isBrowsing = false;

完整代码可以在这里查看:http://pastebin.com/E3wY6U4N

最佳答案

我记得遇到过这个问题,快速解决方法是取消代理并释放浏览器和广告商。因此,假设您的 App Delegate 对每个设置方法都有一个强大的属性,则如下所示:

self.peerAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.myIdentifier discoveryInfo:nil serviceType: service];
self.peerAdvertiser.delegate = self;

self.peerBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.myIdentifier serviceType: service];
self.peerBrowser.delegate = self;

然后当应用程序进入后台(或以其他方式想要停止浏览/广告)时:

self.peerAdvertiser.delegate = nil;
[self.peerAdvertiser stopAdvertisingPeer];
self.peerAdvertiser = nil;

self.peerBrowser.delegate = nil;
[self.peerBrowser stopBrowsingForPeers];
self.peerBrowser = nil;

[self.mainSession disconnect];

我还建议不要在每次应用启动时都创建一个新的 MCPeerID,因为 Multipeer Connectivity 有发现旧对等点的习惯,而你最终会在每次应用时发现“以前的自己”重新启动。

关于ios - MultiPeer MCNearbyBrowserService 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com