Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSBlockOperation调用NSOperation内的方法_Iphone_Ios_Objective C_Nsoperation_Nsoperationqueue - Fatal编程技术网

Iphone NSBlockOperation调用NSOperation内的方法

Iphone NSBlockOperation调用NSOperation内的方法,iphone,ios,objective-c,nsoperation,nsoperationqueue,Iphone,Ios,Objective C,Nsoperation,Nsoperationqueue,我有个问题。 我有以下代码: NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{ [[ClassA sharedInstance] someSingletonMethod:params1]; [ClassB classBMethod:params2]; [self currentClassMethod:params3]; [[NSOperationQ

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

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];
在块中调用单例方法安全吗?在块中调用类方法安全吗?调用“self”方法安全吗

我有以下情况。我正在向服务器发送一批请求:

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;
                }
                //.......
          //.......
       }
基本上,我希望以以下方式构造队列: 1.[上下文创建操作] 2.[将解析从服务器接收的json并将新/修改对象保存到/在上下文中的多个上下文修改操作] 3.[最后的一些方法还将修改上下文,并在最后调用save方法将更改传播到存储,然后使用NSManagedObjectContextDidSaveNotifications将更改传播到其他上下文]

在块中调用单例方法安全吗

这是一个有点复杂的问题,取决于你在你的单例方法中使用了什么

在块中调用类方法安全吗

取决于您在方法内部所做的工作。根据我的经验和我的代码,是的

调用“self”方法是否节省


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

为什么不安全呢?您是否遇到过任何问题(异常或崩溃)?基本上我想做这样的事情:@RuiAAPeres您可以添加更多关于内存泄漏原因的信息吗?这条边在哪里。还有一个问题,这个方法是在该操作块中执行还是在主线程(队列)中执行?@flinth取决于
self
是否对该块具有强引用,并且
self
是否在该块中引用。这足以形成一个保留周期。阻塞操作取决于执行它的队列。
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];
    };