NSBlockOperation在NSOperation中调用一个方法

时间:2013-04-16 10:36:05

标签: iphone ios objective-c nsoperation nsoperationqueue

我有一个问题。 我有以下代码:

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. [一些最终方法也将修改上下文,最后将调用save方法将更改传播到存储,然后使用NSManagedObjectContextDidSaveNotifications到其他上下文]

1 个答案:

答案 0 :(得分:2)

  

在块中调用单例方法是否安全?

这是一个小问题,取决于你在单身人士的方法中的内容。

  

在块中调用类方法是否安全?

取决于您在方法中执行的操作。根据我的经验和我做的代码,是的。

  

是否可以通过调用“自我”方法进行保存?

您正在将self的引用传递给块,这可能会导致内存泄漏。