我有一个可重复的崩溃,以EXC_BREAKPOINT结尾,如下图所示:
重现崩溃的步骤:
编辑:通过调用Progress.cancel()来重现崩溃的另一种方法步骤:
我在线上的代码didFinishReceivingResourceWithName:
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?) {
// transfer to local URL
MusicDownloadRequestor.sharedInstance.finishReceivingSongUploadAtLocalURL(tempUrl: localURL)
}
看起来我的代码在堆栈跟踪中不被调用...
* thread #25: tid = 0x806ec, 0x0000000100944af4 libswiftFoundation.dylib`static Foundation.DateComponents._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSDateComponents>) -> Foundation.DateComponents with unmangled suffix "_merged" + 96, queue = 'com.apple.MCSession.callbackQueue', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100944af4)
frame #0: 0x0000000100944af4 libswiftFoundation.dylib`static Foundation.DateComponents._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSDateComponents>) -> Foundation.DateComponents with unmangled suffix "_merged" + 96
frame #1: 0x0000000100114c60 MyAppSwift`@objc NetworkManager.session(MCSession, didFinishReceivingResourceWithName : String, fromPeer : MCPeerID, at : URL, withError : Error?) -> () + 168 at NetworkManager.swift:0
frame #2: 0x00000001a1dda028 MultipeerConnectivity`__79-[MCSession syncCloseIncomingStream:forPeer:state:error:reason:removeObserver:]_block_invoke + 208
frame #3: 0x0000000100c05258 libdispatch.dylib`_dispatch_call_block_and_release + 24
frame #4: 0x0000000100c05218 libdispatch.dylib`_dispatch_client_callout + 16
frame #5: 0x0000000100c12aec libdispatch.dylib`_dispatch_queue_serial_drain + 1136
frame #6: 0x0000000100c08ce0 libdispatch.dylib`_dispatch_queue_invoke + 672
frame #7: 0x0000000100c14e2c libdispatch.dylib`_dispatch_root_queue_drain + 584
frame #8: 0x0000000100c14b78 libdispatch.dylib`_dispatch_worker_thread3 + 140
frame #9: 0x000000018c2a32a0 libsystem_pthread.dylib`_pthread_wqthread + 1288
frame #10: 0x000000018c2a2d8c libsystem_pthread.dylib`start_wqthread + 4
更新#1:将堆栈跟踪添加为文本
更新#2:发现崩溃可能的潜在客户here's another crash with unconditionallyBridgeFromObjectiveC
我认为问题是URL被传递给didFinishReceivingResourceWithName为nil,但参数是非可选的。这是有道理的,因为如果文件无法传输,则URL不会是最终的休息位置。有什么方法可以解决这个问题或拦截错误吗?
我认为这是一个Apple bug。有人建议做一个解决方法吗?
答案 0 :(得分:4)
使用混合和匹配功能,我能够将我的MCSessionDelegate转换为Objective-C,然后将参数传递给Swift。检查输入值后,我确认变量&#34; localURL&#34; URL类型实际上是nil,这意味着它应该被声明为可选。我向Apple提交了一份错误报告。
作为解决方法,请在Objective-C中编写MCSessionDelegate。让Objective-C和Swift在同一个项目this page explains it very well中协同工作有几个步骤。
这是我的代码。
我的MCSessionDelegate标题:
#import <Foundation/Foundation.h>
#import <MultipeerConnectivity/MultipeerConnectivity.h>
@class NetworkMCSessionTranslator;
@interface NetworkMCSessionDelegate : NSObject <MCSessionDelegate>
-(instancetype)initWithTranslator:(NetworkMCSessionTranslator*) trans;
@end
我的MCSessionDelegate实施文件:
#import "NetworkMCSessionDelegate.h"
#import "MusicAppSwift-Swift.h"
@interface NetworkMCSessionDelegate()
@property (nonatomic) NetworkMCSessionTranslator *translator;
@end
@implementation NetworkMCSessionDelegate
/*
create a NetworkMCSessionDelegate and pass in a swift translator
*/
-(instancetype)initWithTranslator:(NetworkMCSessionTranslator*) trans{
if(self = [super init]){
self.translator = trans;
}
return self;
}
/*
Indicates that an NSData object has been received from a nearby peer.
*/
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
[self.translator networkSession:session didReceive:data fromPeer:peerID];
}
/*
Indicates that the local peer began receiving a resource from a nearby peer.
*/
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progres{
[self.translator networkSession:session didStartReceivingResourceWithName:resourceName fromPeer:peerID with:progres];
}
/*
Indicates that the local peer finished receiving a resource from a nearby peer.
*/
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
[self.translator networkSession:session didFinishReceivingResourceWithName:resourceName fromPeer:peerID at:localURL withError:error];
}
/*
Called when the state of a nearby peer changes.
*/
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
[self.translator networkSession:session peer:peerID didChange:state];
}
/*
Called when a nearby peer opens a byte stream connection to the local peer.
*/
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID{
// not expecting to see any of this
}
@end
我的Swift文件收集代表的流量:
import Foundation
import MultipeerConnectivity
@objc class NetworkMCSessionTranslator: NSObject{
public func networkSession(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState){
...
}
public func networkSession(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID){
...
}
public func networkSession(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?){
// !!! Notice localURL is now an OPTIONAL !!!
}
public func networkSession(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress){
...
}
}
祝你好运!
答案 1 :(得分:1)
只是让人们知道xcode 9 beta 1的发布已经修复了这个bug。根据文档和我的测试,at localurl参数现在是可选的https://developer.apple.com/documentation/multipeerconnectivity/mcsessiondelegate/1406984-session
只需更新到xcode9的最新版本即可解决此问题。
答案 2 :(得分:1)
在iOS 7和xcode 8及更高版本中,使用localURL作为可选
更改您的功能使用此功能
func session(_ session: MCSession, didFinishReceivingResourceWithName
resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?,
withError error: Error?) {
if (localURL != nil) {
//enter code here
}
}
而不是
func session(_ session: MCSession, didFinishReceivingResourceWithName
resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL,
withError error: Error?) {
}
所以,这个函数解决了我的崩溃问题。 如果在xcode 8中显示任何警告,请忽略它。