2016-06-29 31 views
0

我有一個類方法內的下一個問題,當實現一些長期運行的過程,那就是傳遞的參數似乎被釋放。使用方法參數之前,它被釋放使用

我在課堂外調用該方法。

+ (void)downloadVideoForExercise:(Exercise *)exercise{ 

    dispatch_queue_t serialQueue = dispatch_queue_create("com.blah.queue", DISPATCH_QUEUE_SERIAL); 

    dispatch_async(serialQueue, ^{ 

     [self isMaximumDownloadCapacityReached:^(BOOL success) { 
      if (success) { 
       return; 
      } 
     }]; 

     [self shouldDownloadVideoForExercise:exercise 
          andCompletionBlock:^(BOOL success) { 
           if (success == NO) { 
            return; 
           } 
          }]; 

     [NetworkManager downloadFileFromURL:exercise.videoServerURL 
          andSaveLocally:YES 
           andCompletion:^(BOOL success) { 

            dispatch_async(dispatch_get_main_queue(), ^{ 
             RLMRealm *realm = [RLMRealm defaultRealm]; 
             if (![realm inWriteTransaction]) { 
              [realm beginWriteTransaction]; 
             } 
             if (success) { 
              //save the new local link 
              NSString *videopath = [NetworkManager getVideoLocalURLFromServerURLForExercise:exercise.videoServerURL]; 
              exercise.videoLocalURL = videopath; 
              //change flag for ExerciseVideoDownloadState 
              exercise.videoDownloadState = isDownloadCompleted; 
             }else{ 
              //change flag for ExerciseVideoDownloadState 
              exercise.videoDownloadState = isNotDownloaded; 
             } 
             if ([realm inWriteTransaction]) { 
              [realm commitWriteTransaction]; 
             } 
            }); 

           }]; 

    }); 

} 

我已經想過使用傳遞的參數與__strong或__block 但沒有成功。 提前感謝您提供的任何幫助。

+1

你指哪個參數?這將有所幫助。 –

+0

我指的是傳遞給方法的參數,在這種情況下是對象:'(Exercise *)exercise' –

+1

你是什麼意思「似乎被解除分配」? – newacct

回答

0

該問題是由我正在使用的庫生成的,Realm。 問題在於,對象的所有更改都是由某些自定義內存句柄的C++類的後面的句柄,所以當我調用該對象時,它不是返回一個零值,而是一個指向C++對象的指針,該對象是做這份工作。圍繞這個問題這樣做:

- (void)downloadVideoForExercise:(Exercise *)exercise{ 
    if (!serialQueue) { 
     serialQueue = dispatch_queue_create("com.blah.queue", DISPATCH_QUEUE_SERIAL); 
    } 

    NSString *url = exercise.videoServerURL; 

    dispatch_async(serialQueue, ^{ 

     [self isMaximumDownloadCapacityReached:^(BOOL success) { 
      if (success) { 
       return; 
      } 
     }]; 

     [self shouldDownloadVideoForExercise:exercise 
          andCompletionBlock:^(BOOL success) { 
           if (success == NO) { 
            return; 
           } 
          }]; 

     [self downloadFileFromURL:url 
          andSaveLocally:YES 
           andCompletion:^(BOOL success, NSString *localURL) { 

            dispatch_async(dispatch_get_main_queue(), ^{ 
             RLMRealm *realm = [RLMRealm defaultRealm]; 
             if (![realm inWriteTransaction]) { 
              [realm beginWriteTransaction]; 
             } 
             if (success) { 
              //save the new local link 
              NSString *videopath = [self getVideoLocalURLFromServerURLForExercise:exercise.videoServerURL]; 
              exercise.videoLocalURL = videopath; 
              //change flag for ExerciseVideoDownloadState 
              exercise.videoDownloadState = isDownloadCompleted; 
             }else{ 
              //change flag for ExerciseVideoDownloadState 
              exercise.videoDownloadState = isNotDownloaded; 
             } 
             if ([realm inWriteTransaction]) { 
              [realm commitWriteTransaction]; 
             } 
            }); 

           }]; 

    }); 

} 
相關問題