【问题标题】:How to create GCD block with conditional Timer for background thread in iOS?如何在iOS中为后台线程创建带有条件定时器的GCD块?
【发布时间】:2014-06-06 06:05:37
【问题描述】:

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

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

-(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.

        });
    });

}

【问题讨论】:

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


    【解决方案1】:

    你为什么不把 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 文件中上传所有视频。如果您一直打算以这种方式阅读需要上传的视频,则应保存已上传的视频,以免重复上传。

    希望有帮助:)

    【讨论】:

    • 你能告诉我怎么称呼它[self startThreadForUpload];在完成块延迟 5 分钟后?
    • 已更新以添加内联代码,希望能更好地解释我认为您应该使用的方法
    • 谢谢你回复晚了..但我已经这样做并解决了我的问题.. tnaks
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多