【问题标题】:How to make a NSURLSession repeat at a certain interval?如何让 NSURLSession 以一定的间隔重复?
【发布时间】:2015-04-20 11:55:41
【问题描述】:

在不使用NSTimer 对象的情况下让NSSession 在一定间隔内重复自己很有趣,我正在寻找是否可以设置我忽略的NSURLRequestNSURLSessionConfiguration 的某些属性?

【问题讨论】:

  • 你想在什么条件下重复?有什么活动吗?或更改某些变量的值?
  • 所以如果我在某个视图上,我只想每隔一段时间(比如说 5 分钟)向服务器发一个帖子并检索一些数据,我对后台获取不感兴趣
  • 既然你确定了时间间隔,你为什么不用NSTimer来做呢。您对此有什么问题吗?

标签: ios objective-c nsurlsession nsurlsessionconfiguration


【解决方案1】:

如果你不想使用 NSTimer,你可以使用 long pool 技术,你可以在其中配置 timeoutinterval。

(void)longPoll
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest *request    = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

// --- Your code ---

[request release];
[pool drain];

[self performSelectorInBackground:@selector( longPoll ) withObject:nil];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多