我有一個問題。 我有以下代碼: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到其他上下文]
爲什麼它不安全?你有沒有遇到過任何問題(例外或崩潰)? –