2012-07-17 34 views
0

在iOS中我需要調用的時候同樣的方法數,但應用程序必須等待,直到方法完成了它的第一個任務。 (我不能使用布爾值來檢查函數是否正在運行)。的iOS排隊上傳任務

我怎樣才能排隊請求,或等待,直到它以前的任務已經完成?有沒有辦法可以使用NSOperation或NSThread?

感謝

這是我的方法,我需要調用多次

- (void)fetchJobs 
{ 

    dispatch_queue_t queueLocal = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_group_t groupLocal = dispatch_group_create(); 

    UIApplication *app = [UIApplication sharedApplication]; 

    NSTimeInterval backgroundTime = app.backgroundTimeRemaining; 
    NSLog(@"Background task remain time: %g seconds", (double)backgroundTime); 


    [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_START" object:nil]; 

    self.assetUpdateTaskID = [app beginBackgroundTaskWithExpirationHandler:^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      [self endTask]; 
     }); 
    }]; 

    dispatch_group_async(groupLocal, queueLocal, ^{ 

     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Files" inManagedObjectContext:self.managedObjectContext]; 

     //Setup the fetch request 
     NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
     [request setEntity:entity]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat: @"uploaded like %@", [NSString stringWithFormat:@"0"]]; 
     [request setPredicate:predicate]; 

     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES]; 
     [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

     NSError *error = nil; 

     //fetch the records and handle an error 
     NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:request error:&error]; 


     if (fetchResults == nil) { 
      NSLog(@"File Data fetching error : %@", [error localizedDescription]); 
     } 
     else{ 
      if ([fetchResults count] > 0) {    

       dispatch_apply([fetchResults count], queue, ^(size_t i){ 
        NSLog(@"Loop count %zu", i); 

        Files *file = [fetchResults objectAtIndex:i]; 
        self.manageObjectForFiles = file; 
        NSLog(@"Fetched File details -> Name: %@ & status: %@" , file.fileName, file.uploaded);      

        BOOL uploaded = [self filePosting:file.fileName]; 


        if (uploaded) { 

         [self.managedObjectContext deleteObject:file]; 
         NSError *error; 

         if (![self.managedObjectContext save:&error]) { 
          NSLog(@"File table update error : %@", [error description]); 
         } 
        }      

       }); 
       /* 
       for (int i = 0; i < [fetchResults count]; i++) {      

       }*/ 
      }    
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

      int remainJobs = [self fetchFileTableCount]; 
      if (remainJobs > 0) { 

       [UIApplication sharedApplication].applicationIconBadgeNumber = remainJobs; 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_REMAIN" object:nil]; 
      } 
      else{ 
       [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_COMPLETED" object:nil]; 
       NSLog(@"-----------------------------------------------"); 
       NSLog(@"There are no more uploads"); 
       NSLog(@"-----------------------------------------------"); 
      } 
      [self endTask];          

     }); 

    }); 

    dispatch_release(groupLocal); 

} 
+0

,而不是調用方法倍數次可你剛纔創建的方法中的循環?所以你只需要調用一次? – 2012-07-17 17:39:22

+0

這是否需要在後臺線程中運行?方法默認是串行運行的... – 2012-07-17 17:43:28

+0

是的,這必須在後臺線程上運行。 – Chinthaka 2012-07-17 18:54:36

回答

1

你會如果調用必須是連續的,如果他們能夠同時並沒有說。如果同時你可以使用的NSOperation的mainQueue,暫停,添加所有,但第一個呼叫,然後在你的方法,在最後,看是否暫停隊列,如果是恢復它。

使用Grand Central Dispatch(CGD),您可以更輕鬆地創建一個串行調度隊列,並且可以執行相同的操作 - 掛起它,除第一個包裝在塊中的所有呼叫外,再添加第一個呼叫取消暫停隊列。如果你需要這個主線程上運行,你可以有串行隊列綁在主GCD隊列。的

+0

我已添加我的源代碼。這個函數在viewDidAppear方法中被調用。 – Chinthaka 2012-07-17 19:01:07