2

以下是我在从 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.

        });
    });

}
4

1 回答 1

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

希望有帮助:)

于 2014-06-06T07:18:07.200 回答