Reputation: 181
I have a question. I have the following code:
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];
Is it safe to call singleton methods in a block? Is it safe to call class methods in a block? Is it safe to call "self" methods?
I have a following situation. I'm sending a batch of requests to server:
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");
Here how I'm handling responses. I'm creating operations to handle completed requests.
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;
}
//.......
//.......
}
After I created an array with operations which will handle (process and update database) JSON that I pulled from the server:
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];
};
Basically I want to construct the queue in such way: 1. [context create operation] 2. [multiple context modify operations that will parse json received from server and save new/modify objects to/in context] 3. [some final methods that will also modify context and, at the end, that will call a save method to propagate changes to the storage and then using NSManagedObjectContextDidSaveNotifications to other context]
Upvotes: 1
Views: 1024
Reputation: 25927
Is it safe to call singletons methods in block?
It's a bit board question, depends on what you inside your singleton's method.
Is it safe to call class methods in block?
Depends on what you do inside your method. From my experience and the code I do, yes.
Is it save to call "self" methods?
You are passing a reference of self
to the block, which might cause a memory leak.
Upvotes: 2