使用单独的线程时,Delegate方法不会调用

时间:2014-07-21 04:32:06

标签: ios objective-c multithreading

我使用下面的代码与Web服务器进行同步。由于getUnsyncTicketsFromServer方法在主队列中调用,它正在工作但UI冻结了一会儿。我试图在单独的队列中进行。然后它不会调用委托方法(requestReturnedData)。

[NSTimer scheduledTimerWithTimeInterval:320.0 target:self selector:@selector(syncTickets) userInfo:nil repeats:YES];

-(void)syncTickets{

   [self sendUnsyncedTickets];

   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
          [self getUnsyncTicketsFromServer];
       });
   });


}    

-(void)requestReturnedData:(NSData *)data{

 }

编辑 -

-(void)getUnsyncTicketsFromServer{

    ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
    serviceConnector.delegate = self;
    [serviceConnector getTicketsFromServer];
    dataMethod = @"get";

}

1 个答案:

答案 0 :(得分:3)

@implementation YourClass () {
    ServiceConnector *_serviceConnector;
}

...

-(void)getUnsyncTicketsFromServer {
    _serviceConnector = [[ServiceConnector alloc] init];
    _serviceConnector.delegate = self;
    [_serviceConnector getTicketsFromServer];
    dataMethod = @"get";
}

如果您正在使用arc,我认为您的serviceConnector必须被释放,因此将该对象存储为实例变量。