2013-04-16 69 views
1

我有一個問題。 我有以下代碼:NSBlockOperation調用NSOperation中的方法

NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{ 

     [[ClassA sharedInstance] someSingletonMethod:params1]; 
     [ClassB classBMethod:params2]; 
     [self currentClassMethod:params3]; 

     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"kSNotificationName" object:nil]; 
     }]; 
    }]; 

[self.myOperationQueue addOperation:op]; 

它是安全的調用塊單的方法呢?在塊中調用類方法是否安全?稱「自我」方法是否安全?

我有以下情況。我正在向服務器發送一批請求:

AFHTTPClient *client=[[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]]; 
[client registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
[client enqueueBatchOfHTTPRequestOperations:reqOps progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) { 
    NSLog(@"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations); 
    [[PTDictionaryUpdate sharedInstance] debugPrint:[NSString stringWithFormat:@"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations]]; 
} completionBlock:^(NSArray *operations) { 
    NSLog(@"operations finished"); 

這裏是我如何處理響應。 我正在創建操作來處理已完成的請求。

for (int i=0; i<[operations count]; i++) 
    { 
     AFJSONRequestOperation *operation=[operations objectAtIndex:i]; 
     if ((operation.error==nil) && (operation.response.statusCode==200)) 
     { 
      id JSON=operation.responseJSON; 
      int handleMethodIndex=-1; 
      for (int j=0; j<[urls count]; j++) 
      { 
       if ([operation.request.URL isEqual:[urls objectAtIndex:j]]) 
       { 
        handleMethodIndex=j; 
       }; 
      }; 

      switch (handleMethodIndex) { 
       case 0: 
       { 
        //[self countryUpdate:JSON]; 

        NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(countryUpdate:) object:JSON]; 
        [invOp setQueuePriority:NSOperationQueuePriorityLow]; 
        [handleJSONOperations addObject:invOp]; 
        break; 
       } 
       case 1: 
       { 
        //[self regionsUpdate:JSON]; 

        NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(regionsUpdate:) object:JSON]; 
        [invOp setQueuePriority:NSOperationQueuePriorityLow]; 
        [handleJSONOperations addObject:invOp]; 
        break; 
       } 
       //....... 
      //....... 
     } 

後我創建操作,它將處理(處理和更新數據庫)JSON的陣列,我從服務器拉:

NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{ 

     //first we need to tether countries, regions and cities 
     [[PTDataTetherer sharedInstance] tetherCountriesRegionsCitiesInContext:self.updateContext]; 

     //generating fake agencies 
     //[PTFakeAgencyGenerator generateAgenciesInContext:context]; 

     //generating fake clients 
     //[PTFakeClientGenerator generateClientsInContext:context]; 

     //generating fake reports 
     [[PTFakeReportGenerator sharedInstance] generateReportsInContext:self.updateContext]; 

     //generating fake presentations 
     [[PTFakePresentationGenerator sharedInstance] generatePresentationsInContext:self.updateContext]; 


     //tethering 
     [[PTDataTetherer sharedInstance] tetherAgenciesWithOthersInContext:self.updateContext]; 
     [[PTDataTetherer sharedInstance] tetherClientsWithOthersInContext:self.updateContext]; 
     [[PTDataTetherer sharedInstance] tetherEventsWithOthersInContext:self.updateContext]; 
     [[PTDataTetherer sharedInstance] tetherPresentationFoldersWithImagesInContext:self.updateContext]; 

     [self saveContext]; 

     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"kSynchronizationFinishedNotification" object:nil]; 
     }]; 
    }]; 
    [op setQueuePriority:NSOperationQueuePriorityLow]; 
    if ([handleJSONOperations count]==0) 
    { 
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"kSynchronizationFinishedNotification" object:nil]; 
     }]; 
    } 
    else 
    { 
     [self.serverUpdateQueue addOperation:updateContextCreateOperation]; 
     [handleJSONOperations addObject:op]; 
     [self.serverUpdateQueue addOperations:handleJSONOperations waitUntilFinished:NO]; 
    }; 

基本上我要建構隊列以這樣的方式: 1. [上下文創建操作] 2. [多個上下文修改操作,將解析從服務器收到的json並在上下文中保存新的/修改對象] 3. [一些最終的方法也將修改上下文,並在最後,這將調用一個保存方法將更改傳播到存儲,然後使用NSManagedObjectContextDidSaveNotifications到其他上下文]

+0

爲什麼它不安全?你有沒有遇到過任何問題(例外或崩潰)? –

回答

2

是否可以在塊中調用單例方法?

這是一個有點板的問題,取決於你在你的單身人士的方法。

在塊中調用類方法是否安全?

取決於你在你的方法中做了什麼。根據我的經驗和我所做的代碼,是的。

調用「自我」方法是否存儲?

您正在將self的引用傳遞給塊,這可能導致內存泄漏。

+0

基本上我想做這樣的事情: – user1897723

+0

你想做什麼? – Peres

+0

我已決定編輯我的答案=) – user1897723

相關問題