2014-05-13 20 views
0

我用多等連接框架的 -如何安全地停止與Mutipeer連接發送圖像

sendResourceAtURL:withName:toPeer:withCompletionHandler:

方法發送image.But我發現一個問題:如果圖像發送還沒有完成,我調用MCSession的斷開方法,應用程序崩潰。所以我想知道如何安全地停止使用多對等連接發送圖像。

- (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:@"%@[email protected]#%@",[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; 
} 
+0

你能發表一些代碼嗎?什麼是你的完成塊? – ChrisH

+0

@ChrisH我創建了自定義類的新實例並調用了自定義委託即時的方法。 –

+0

我的猜測是在完成塊中引用self。 http://stackoverflow.com/questions/4352561/retain-cycle-on-self-with-blocks – ChrisH

回答

1

您必須在圖像上設置一個布爾變量或通知發送completionHandler比檢查時,你得到的是BOOL值正確,或者通知,如果它是正確的比你可致電MCSession的斷開方法。