
有没有办法可以检查runloop是否可以调用同步方法,如果不是,则发送同步请求(如果不是在命令行工具中使用).
或者,是否有一种方法可以始终使用异步方法但强制应用程序在异步请求完成之前不会退出?
我注意到了this,但我宁愿不必把这个电话放在图书馆外面.
谢谢你的帮助
解决方法Alternately,is there a way to always use the asynchronous method but force the application to not exit until the async request has finished?
非常简单:
int main(int argc,char *argv[]){ // Create request,delegate object,etc. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:YES]; CFRunLoopRun(); // ...} 我在这里使用CFRunLoopRun(),因为当委托确定连接完成时,可以稍后停止它:
- (voID)connectionDIDFinishLoading:(NSURLConnection *)connection { // This returns control to wherever you called // CFRunLoopRun() from. CFRunLoopStop(CFRunLoopGetCurrent());}- (voID)connection:(NSURLConnection *)connection dIDFailWithError:(NSError *)error { NSLog(@"Error: %@",error); CFRunLoopStop(CFRunLoopGetCurrent());} 另一种选择是在while循环中使用 – [NSRunLoop runUntilDate:],并让委托设置一个“停止”标志:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:YES];while( ![delegate connectionHasFinished] ){ [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1];} 总结 以上是内存溢出为你收集整理的objective-c – 检查NSRunLoop并使用适当的NSURLConnection方法全部内容,希望文章能够帮你解决objective-c – 检查NSRunLoop并使用适当的NSURLConnection方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)