1

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

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 到其他上下文]

4

1 回答 1

2

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

这是一个有点板的问题,取决于你在单例方法中的内容。

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

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

调用“自我”方法是否可以节省?

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

于 2013-04-16T10:44:13.810 回答