方法参数在使用之前被释放

The method parameter is deallocated before it is used

我在 class 方法中遇到下一个问题,当执行几个长的 运行 过程时,传递的参数似乎被释放了。

我正在从 class 外部调用该方法。

+ (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 一起使用 但没有成功。 预先感谢您提供的任何帮助。

问题是由我正在使用的库 Realm 引起的。 问题是对象的所有更改都是由一些 C++ class 在后面处理的,它执行一些自定义内存句柄,所以当我调用对象时,它返回的不是 nil 值,而是指向 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];
                                      }
                                  });

                              }];

    });

}