2

다음은 plist 파일에서 백그라운드 스레드 읽기에 비디오를 업로드하는 나의 방법입니다.iOS의 백그라운드 스레드 용 조건부 타이머로 GCD 블록을 만드는 방법은 무엇입니까?

이제 내가 필요한 것은 plist에서 모든 항목을 읽고 첫 번째 블록의 실행을 완료했습니다. 거기에 새로운 항목이 plist 파일에 들어 왔는지 확인하고 싶습니다. 몇 분 후에 startThreadForUpload 어느 누구도 저에게 어떻게 제안 할 수 있습니까? 그것을 실행 해 계속해서 그래서 지금 난 그냥 새로운 변경 가능한 사전에 다시 왜 그냥 PLIST를 읽지 않는다 ...

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

     }); 
    }); 

} 

답변

1

을 완료 블록에서 같은 방법을 호출 한 다음 이미 키 당신을 위해 개체를 제거 처리하고 프로세스를 반복합니다.

모든 업로드가 완료 될 때까지 반복적으로 호출 할 수 있도록 실제 업로드 기능을 새 방법으로 리팩터링해야합니다. 그런 다음 지연 후 원래 선택기를 다시 수행하거나 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 파일의 모든 비디오를 업로드한다는 것입니다. 업로드해야하는 비디오를 항상 읽는 방법 인 경우 두 번 업로드하지 않으려면 이미 업로드 된 비디오를 저장해야합니다.

도움이 되길 바랍니다. :)

+0

[self startThreadForUpload]; 완료 블록에서 5 분 동안 지연 후? –

+0

코드를 인라인으로 추가하도록 업데이트되었으므로 희망적으로 접근 방법을 더 잘 설명합니다. – Sam

+0

고맙습니다. 내가 늦게 답장했지만 .. 내 문제는이 문제를 해결하고 해결했습니다. tnaks –