HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 长的投票在客观C大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序使用API​​来获取网站上的实时更新.他们使用他们所说的 long-polling technique

一旦你得到回应,这个强制执行一个请求回到服务器.在iphone应用程序中最好的方法是什么?这最终必须在后台运行.

解决方法

这正是NSURLConnection sendSynchronousRequest的完美用例:

- (void) longPoll {
    //create an autorelease pool for the thread
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    //compose the request
    NSError* error = nil;
    NSURLResponse* response = nil;
    NSURL* requestUrl = [NSURL URLWithString:@"http://www.mysite.com/pollUrl"];
    NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl];

    //send the request (will block until a response comes back)
    NSData* responseData = [NSURLConnection sendSynchronousRequest:request
                            returningResponse:&response error:&error];

    //pass the response on to the handler (can also check for errors here,if you want)
    [self performSelectorOnMainThread:@selector(dataReceived:) 
          withObject:responseData waitUntilDone:YES];

    //clear the pool 
    [pool drain];

    //send the next poll request
    [self performSelectorInBackground:@selector(longPoll) withObject: nil];
}

- (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
}

或者,您可以使用异步I / O和委托回调来完成同样的事情,但在这种情况下,这真的会是一种愚蠢的事情.

大佬总结

以上是大佬教程为你收集整理的iphone – 长的投票在客观C全部内容,希望文章能够帮你解决iphone – 长的投票在客观C所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: