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

时间:2014-07-31 05:42:53

标签: ios multithreading delegates

我正在使用以下代码将同步电话数据库与网络数据库联系起来。但是 - connectionDidFinishLoading:方法没有调用。如果我用dispatch_sync而不是dispatch_async它可以工作。但我想在后台线程中运行它。

 _serialQueue = dispatch_queue_create("com.company.myApp.SerialQueue", DISPATCH_QUEUE_PRIORITY_DEFAULT);


   __weak typeof(self) weakSelf = self;
   dispatch_async(  _serialQueue, ^{
         [weakSelf sendUnsyncedTickets];
   });

   dispatch_async(  _serialQueue, ^{
         [weakSelf getUnsyncTicketsFromServer];
   });


 -(void)sendUnsyncedTickets{

   serviceConnector = [[ServiceConnector alloc] init];
   serviceConnector.delegate = self;
   [serviceConnector postTest];
   dataMethod = @"send";
   serviceConnector = nil;

 }

  -(void)getUnsyncTicketsFromServer{

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

    serviceConnector = nil;

 }

--- ServiceConnector.m

-(void)getTicketsFromServer{

receivedData = [NSMutableData dataWithCapacity: 0];

dataMethod = @"get";


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/not-redeem.json"]];


[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

   if(!connection){
       NSLog(@"Connection Failed");
       receivedData = nil;
    }

}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

}

0 个答案:

没有答案