ios - 如何在 iOS 中为后台线程创建带条件计时器的 GCD block ?

标签 ios objective-c grand-central-dispatch nstimer background-thread

以下是我从 plist 文件读取后台线程上传视频的方法。

现在我需要的是,一旦他们从 plist 中读取了所有条目并完成了第一个 block 的执行,我想检查完成 block 中是否有任何新条目进入 plist 文件..如果不是,则调用 startThreadForUpload 几次之后。所以有人可以建议我该怎么做吗?现在我只是在完成 block 中调用相同的方法,所以它继续运行......

-(void)startThreadForUpload{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        assetManager =[AssetManager sharedInstance];
        NSDictionary *videoListDict=  [assetManager getAllVideoFromPlist];
        NSArray *videoListKeyArray=[videoListDict allKeys];

        if(videoListKeyArray.count!=0)
            {

            for(NSString *key in videoListKeyArray){
                NSData *videoData = [videoListDict objectForKey:key];
                Video *vidObject = (Video *)[NSKeyedUnarchiver unarchiveObjectWithData:videoData];

             amazonManger=[AmazonManager sharedInstance];

                [amazonManger uploadVideoWithVideoName:vidObject.videoName IsImage:NO VideoObject:vidObject];
                [amazonManger uploadVideoWithVideoName:vidObject.thumbImageName IsImage:YES VideoObject:vidObject];

            }


        }
        dispatch_async(dispatch_get_main_queue(), ^(void) {

            //Stop your activity indicator or anything else with the GUI
            //Code here is run on the main thread

            [self startThreadForUpload];
            // WARNING! - Don't update user interfaces from a background thread.

        });
    });

}

最佳答案

您为什么不再次将 plist 读入一个新的可变字典,然后删除您已处理的键的对象并重复该过程。

您应该将实际的上传功能重构为一个新方法,这样您就可以递归调用该方法,直到所有视频都上传完毕。然后您可以在延迟后或使用 dispatch_after 再次执行原始选择器。

将 for 循环重构为一个名为 uploadVideos: 的新方法,并像这样调用它:

- (void)startThreadForUpload
{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    assetManager = [AssetManager sharedInstance];
    NSDictionary *videoListDict = [assetManager allVideoFromPlist];

    // call the new method defined below to upload all videos from plist
    [self uploadVideos:videoListDict.allValues];

    // create a mutable dictionary and remove the videos that have already been uploaded
    NSMutableDictionary *newVideoListDict = [assetManager getAllVideoFromPlist].mutableCopy;
    [newVideoListDict removeObjectsForKeys:videoListDict.allKeys];

    if (newVideoListDict.count > 0) {
      // new videos, so upload them immediately
      [self uploadVideos:newVideoListDict.allValues];
    }

    // start another upload after 300 seconds (5 minutes)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(300 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [self startThreadForUpload];
    });
  });
}

- (void)uploadVideos:(NSArray *)videos
{
  AmazonManager *amazonManger = [AmazonManager sharedInstance];

  for (NSData *videoData in videos) {
    Video *vidObject = (Video *)[NSKeyedUnarchiver unarchiveObjectWithData:videoData];

    // call a method defined in a category, described below
    [amazonManager uploadVideo:vidObject];
  }
}

您应该在 AmazonManager 上定义一个类别以保持良好的关注点分离:

// put this in AmazonManager+VideoUpload.h
@interface AmazonManager (VideoUpload)
- (void)uploadVideo:(Video *)video;
@end

// put this in AmazonManager+VideoUpload.m
@implementation AmazonManager (VideoUpload)

- (void)uploadVideo:(Video *)video
{
  [self uploadVideoWithVideoName:video.videoName IsImage:NO VideoObject:video];
  [self uploadVideoWithVideoName:video.thumbImageName IsImage:YES VideoObject:video];
}

@end

现在的问题是,每次调用 startThreadForUpload 方法时,它都会从您的 plist 文件中上传所有视频。如果您总是打算以这种方式阅读需要上传的视频,您应该保存哪些视频已经上传,以避免上传两次。

希望对您有所帮助:)

关于ios - 如何在 iOS 中为后台线程创建带条件计时器的 GCD block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075257/

相关文章:

ios - 将发出数组的 SignalProducer 转换为发出原始数组所有元素的 SignalProducer

iOS SDK CGAffineTransform 角度问题

ios - StoryBoard - UITabBarController + UINavigationController - 从一个导航 Controller 跳转到另一个

ios - 为什么GCD不起作用?

swift - swift 中的并行编程是否消除了值类型优化?

ios - Swift 中的 KVO,observeValueForKeyPath 的有趣问题

iphone - 当 iPhone 应用程序在后台运行时显示弹出窗口 (UIAlertView)

objective-c - NSJSONSerialization解析特殊字符

iphone - 如何更改 UITextField clearButton 的颜色?

ios - 如何知道 dispatch_async 正在运行 ios