Iphone 在IOS中冻结UI
下面的代码正在冻结我的UI。不能做任何动作Iphone 在IOS中冻结UI,iphone,ios,objective-c,ipad,Iphone,Ios,Objective C,Ipad,下面的代码正在冻结我的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;
- (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];
}
有人能告诉我它的确切原因吗?或者有没有其他方法可以使用类似的代码继续轮询。您正在创建一个无限循环:
longCall
callsdataReceived
callslongCall
等……您到底想做什么。在longPool和dataReceived之间存在无限循环
应该有一个机制,你可以停止这个电话,你可以使用它
@autorelease {} block for create autorelease pool in ARC Enabled project and
NSAutoReleasePool class obj for Without ARC.
无需调用
dataReceived:
在dispatch\u async(dispatch\u get\u main\u queue(),^{
检查您在[self-ParseJson:dict]中执行的操作;只有它可以冻结UI。