- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个看起来像这样的 NSURLSession
,我想做的是,如果没有返回数据,那么我希望它一次又一次地尝试,最多可以说 8 秒或尝试 3 次,然后如果仍然没有任何内容,我将让它不返回任何数据。这是我现在拥有的,
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlIn] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// handle response
//use my data
// if no data I want to try again?
}] resume];
所以我希望它尝试 3 次或直到 8 秒过去。我该怎么做,
提前感谢您的帮助。
最佳答案
好吧,我快完成了,我还有一点要用定时器来解决。这是我的。
我有属性
NSInteger attemptInt;
NSInteger _counter;
NSDate *date = [NSDate date];
然后这两个方法用于我的计时器
- (void)startCountdown
{
_counter = 8;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
_counter--;
if (_counter <= 0) {
[timer invalidate];
NSLog(@"8 sec passed");
}
}
然后这是我获取数据的地方。
- (void)getMenuItems:(NSString*)urlIn{
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlIn] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//One problem I am facing is I have to call timer for main thread
if ([date timeIntervalSinceNow] <= 8 && [response expectedContentLength] == 0) {
attemptInt++;
[self getMenuItems:url];
}
if (attemptInt >= 3) {
NSLog(@"NO DATA");
}
else if ([response expectedContentLength] == 0) {
NSLog(@"TRY AGAIN");
attemptInt++;
[self getMenuItems:url];
}
else {
//GOT MY DATA :)
self.mealdata=[[MealData alloc]init:data];
}
所以我已经完成了尝试,我是否设置了计时器我只需要一些帮助来完成检查是否先过了 8 秒?
关于ios - 发送多个 NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110675/
我编写了一个提供一些数据的 REST 服务。它受密码保护。 我正在尝试编写一个后台进程来获取数据并将其填充到我在应用程序中拥有的 sqlLite 数据库中。 我最初是在没有身份验证的情况下使用的: -
我想要一个 API key 并验证用户身份。我在控制台中看到消息“ session URL 任务成功:HTTP 200”。不幸的是,我总是收到页面“{”消息“:”的消息。此请求“}”的授权已被拒绝。我
我为 NSURLSession 创建了类别,然后我遇到了一个问题。在 iOS 7 中,[[NSURLSession sharedSession] isKindOfClass:[NSURLSession
如何从 NSHTTPCookieStorage.sharedHTTPCookieStorage 中删除所有 cookie?我知道删除单个指定 cookie 的唯一方法,但是,这些 cookie 是由
我正在尝试在应用程序进入后台(例如从主页按钮按下)的情况下正确处理进程内 NSURLSessionTasks。我目前正在采用将进程中的任务复制到后台队列的方法(请参见下面的代码)。然而,我发现后台任务
我是 objective-c 中的新手,使用 iOS 9 Xcode 7.2 并收到此警告“'initWithRequest:delegate:'已弃用:在 iOS 9.0 中首次弃用 - 使用 NS
当我导入 Gmail API 的 GTL.xcodeproj 并运行它时出现上述错误。我正在使用 Xcode 7.1 beta 并且在 Gmail API 二进制文件的头文件中出现了很多错误我们不推荐
我在我的 iOS 应用程序中使用 Alamofire 进行网络连接。我需要在 iOS 7+ 中运行这个应用程序。我要在状态栏中指示网络事件 ,所以我创建了这个结构: struct ActivityMa
我需要从服务器下载视频并保存视频以供稍后观看。 所以我需要下载视频并将其使用自定义名称保存在应用程序的文件系统中,到目前为止我可以下载数据(我猜)但无法存储它。 不,我不想使用额外的扩展框架或其他什么
在一个小型 iOS 应用程序中,我需要从服务器检索一些 JSON(类似于 DNS 请求),并使用此响应来发起进一步的连接。为此,我有一个使用 NSURLSession 处理 JSON 请求的对象。该对
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
当我尝试在网络离线(飞行模式)时启动基本的 NSURLSession 传输时,使用 NSURLSessionConfiguration defaultSessionConfiguration 和 ep
我曾经用 NSURLConnection 做我想做的事. 我会创建一个新的自定义 NSOperation对象 设置我的 NSMutalbeRequest当准备好触发operation女巫会排队操作..
我创建了一个下载任务。我已经使用cancelByProducingResumeData在应用程序中取消了任务,并将其保存到磁盘。当用户重新启动应用程序并单击“恢复”按钮时,我再次通过self.down
即使在使 NSURLSession 无效后,使用 Instruments 运行配置文件,一些名为 TubeManager、HTTPConnectionCache 和 HTTPConnectionCac
我有一段普通的代码,我几乎在我的代码中到处使用: let configuration = URLSessionConfiguration.default let session = URLSessio
我希望有人能够帮助我理解在我的 WatchOS 2 应用程序中尝试实现 NSURLSession 时遇到的问题。 无论出于何种原因,我收到错误; Error Domain=NSCocoaErrorDo
我正在尝试使用 NSURLSession 从 URL 获取 ID,但是 let task = NSURLSession.... 和 task.resume 之间的代码 永远不会执行(我在里面放置了一个
我正在尝试使用 NSURLSession 发出 HTTPRequest。当我设置完整的 url 时,请求返回正确的数据,但是当使用参数时 (NSJSONSerialization.dataWithJS
我是这方面的新手,但过去常常在这里搜索特定的东西,并且由于我找不到特定的答案,也许这里有人可以提供帮助。 我基本上是在学习和构建一个应用程序。 目前我已经构建登录、注册 View Controller
我是一名优秀的程序员,十分优秀!