【问题标题】:how to safely stop sending image with Mutipeer Connectivity如何使用 Multipeer Connectivity 安全地停止发送图像
【发布时间】: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 的显式引用以及对progress ivar 的隐式引用。你需要__weak id weakSelf = self

标签: ios ios7 multipeer-connectivity


【解决方案1】:

您必须在图像发送 completionHandler 上设置一个 BOOL 变量或通知,而不是在您获得正确的 BOOL 值或通知时检查,如果正确,则可以调用 MCSession 的断开方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多