2013-07-24 59 views
0

以下代码冻结了我的UI。不能做任何行动。冻结IOS中的UI

- (void) longPoll { 
    //create an autorelease pool for the thread 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     NSError* error = nil; 
     NSURLResponse* response = nil; 
     NSURL* requestUrl = [NSURL URLWithString:@"myurl"]; 
     NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl]; 

     //send the request (will block until a response comes back) 
     NSData* responseData = [NSURLConnection sendSynchronousRequest:request 
                returningResponse:&response error:&error]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self dataReceived:responseData]; 
     }); 
    }); 
     //compose the request 


     //pass the response on to the handler (can also check for errors here, if you want) 


     //clear the pool 

    } 




- (void) startPoll { 
    //not covered in this example: stopping the poll or ensuring that only 1 poll is active at any given time 
    [self performSelectorInBackground:@selector(longPoll) withObject: nil]; 
} 

- (void) dataReceived: (NSData*) theData { 
    //process the response here 
    NSDictionary *dict=[theData JSONValue]; 
    [self ParseJson:dict]; 
    [self performSelectorInBackground:@selector(longPoll) withObject: nil]; 
} 

任何人都可以给我它的确切原因或任何替代方案来做类似的代码继续轮询。

+0

无需调用'dataReceived的:)'的'dispatch_async内(dispatch_get_main_queue(,^ {' –

+0

检查你在[self ParseJson:dict]中做了什么;只有它可以冻结UI。 – stosha

回答

1

你正在创建一个无限循环:

longCall电话dataReceived调用longCall等....

0

正是你想要做什么。有longPool和dataReceived 之间无限循环应该有机制,其中你停止了此呼叫,您可以使用

@autorelease {} block for create autorelease pool in ARC Enabled project and 
NSAutoReleasePool class obj for Without ARC.