【发布时间】:2014-05-13 06:05:50
【问题描述】:
我使用 Multi-Peer Connectivity 框架的 -
sendResourceAtURL:withName:toPeer:withCompletionHandler:
发送图像的方法。但是我发现一个问题:如果图像发送没有完成并且我调用MCSession的断开方法,应用程序崩溃了。所以我想知道如何安全地停止使用Multi-Peer Connectivity发送图像。
- (Transcript *)sendImage:(NSURL *)imageUrl
{
NSProgress *progress;
// Loop on connected peers and send the image to each
for (MCPeerID *peerID in self.MySession.connectedPeers) {
// Send the resource to the remote peer. The completion handler block will be called at the end of sending or if any errors occur
progress = [self.MySession sendResourceAtURL:imageUrl withName:[NSString stringWithFormat:@"%@!@#%@",[imageUrl lastPathComponent],self.MyUserName] toPeer:peerID withCompletionHandler:^(NSError *error) {
// Implement this block to know when the sending resource transfer completes and if there is an error.
if (error) {
NSLog(@"Send resource to peer [%@] completed with Error [%@]", peerID.displayName, error);
}
else {
// Create an image transcript for this received image resource
Transcript *transcript = [[Transcript alloc] initWithUserName:self.MyUserName imageUrl:imageUrl direction:TRANSCRIPT_DIRECTION_SEND];
[self.delegate updateTranscript:transcript];
}
}];
}
// Create an outgoing progress transcript. For simplicity we will monitor a single NSProgress. However users can measure each NSProgress returned individually as needed
Transcript *transcript = [[Transcript alloc] initWithUserName:self.MyUserName imageName:[imageUrl lastPathComponent] progress:progress direction:TRANSCRIPT_DIRECTION_SEND];
return transcript;
}
【问题讨论】:
-
你能贴一些代码吗?你的完成块中有什么?
-
@ChrisH 我创建了一个自定义类的新实例并调用了一个自定义委托即时的方法。
-
我的猜测是完成块中对 self 的引用。 stackoverflow.com/questions/4352561/…
-
@ChrisH 我用了__weak id weakDelegate = self.delegate;但是问题依旧存在。
-
这不仅仅是委托参考。您在该块中有几个对
self的显式引用以及对progressivar 的隐式引用。你需要__weak id weakSelf = self
标签: ios ios7 multipeer-connectivity