Objective-C AFnetworking:停止请求(Objective-C AFnetworking: stopping a request)
晚上,我正在使用Marvel-API,尝试下载所有角色。
要下载您必须执行多个请求的所有字符,您可以在每个请求中指定限制和偏移量。
所以我将限制设置为最大值100,并且对于每个请求,我将偏移量增加100。
这样做,我做了无限的要求。 当然。
所以我认为当从JSON对象检索到的“results”数组为空时我应该停止。
所以逻辑应该是好的,我一直要求字符100乘100,直到没有更多要检索。
但当然使用网络和异步代码并不总是那么容易。 显然我有了库存。
我确信问题出在这些代码行中:
#pragma mark - Requesting data -(void)getData { NetworkManager *networkManager = [NetworkManager alloc]; while(self.requestMustEnd == false) { NSLog(@"offset: %d", networkManager.offset); AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:networkManager.getUrlPath parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject); [self parseResponseData:responseObject]; } failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [networkManager increaseOffset]; } } #pragma mark - Parsing Method -(void)parseResponseData:(NSDictionary *)responseDictionary { NSArray *marvelArray = [[responseDictionary objectForKey:@"data"] objectForKey:@"results"]; if (marvelArray.count == 0) { self.requestMustEnd = true; } for(NSDictionary* marvel in marvelArray) { Character *currentMarvelEntity = [[Character alloc] initWithMarvel:marvel]; //NSLog(@"currentMarvelEntity %@", currentMarvelEntity.name); [self.marvelCharacters addObject:currentMarvelEntity]; } [self.tableView reloadData]; }
停止请求的关键部分是:
if (marvelArray.count == 0) { self.requestMustEnd = true; }
但是,它仍然永远不会要求。 我确定,这不是if条件。 但可能是因为拥有异步代码,getData func无论是什么继续请求数据。
有小费吗?
Evening, I'm working with the Marvel-API, trying to download all the characters.
To download all the characters you have to do multiple requests, in each request you can specified the limit and the offset.
So I've set the limit at the max of 100 and for every request I increase the offset by 100.
Doing that, I do infinite request. Of course.
So I thought that I should stop when the "results" array retrieved from the JSON object is empty.
So the logic should be good, I keep requesting characters 100 by 100 until there are no more to retrieve.
But of course working with networking and async code isn't always so easy. And obviously I got stocked.
I'm sure that the problems is in these lines of code:
#pragma mark - Requesting data -(void)getData { NetworkManager *networkManager = [NetworkManager alloc]; while(self.requestMustEnd == false) { NSLog(@"offset: %d", networkManager.offset); AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:networkManager.getUrlPath parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject); [self parseResponseData:responseObject]; } failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [networkManager increaseOffset]; } } #pragma mark - Parsing Method -(void)parseResponseData:(NSDictionary *)responseDictionary { NSArray *marvelArray = [[responseDictionary objectForKey:@"data"] objectForKey:@"results"]; if (marvelArray.count == 0) { self.requestMustEnd = true; } for(NSDictionary* marvel in marvelArray) { Character *currentMarvelEntity = [[Character alloc] initWithMarvel:marvel]; //NSLog(@"currentMarvelEntity %@", currentMarvelEntity.name); [self.marvelCharacters addObject:currentMarvelEntity]; } [self.tableView reloadData]; }
The key part to stop the request is:
if (marvelArray.count == 0) { self.requestMustEnd = true; }
But, still, it never end to request. it is not for the if condition, I'm sure. But probably because, having an async code, the getData func no matter what keep requesting data.
Any tips?
原文:
最满意答案
让我们尝试进一步削减这个钱位。 这基本上就是你在做什么。
var month = new Date().getMonth() var nextMonth = function(m) { m++; setMonthSomewhere(m); }; nextMonth(month);
所以会发生的事情是你将遍布任何地方的全局变量传递给
nextMonth()
函数。 函数参数基本上声明了一个局部变量,该变量设置为传入的值。 现在,您将全局month
变量设置为1
,并且nextMonth()
函数的本地完全不同的m
也设置为1
。 然后,将局部变量m
增加到2
,这对全局变量没有影响。相反,只是不传递它。直接操纵全局。
var month = new Date().getMonth() var nextMonth = function() { month++; setMonthSomewhere(month); }; nextMonth();
现在
nextMonth()
直接递增全局month
变量,改变它的值。Lets try to trim this down further to the money bits. Here's basically what you are doing.
var month = new Date().getMonth() var nextMonth = function(m) { m++; setMonthSomewhere(m); }; nextMonth(month);
So what happens is that you pass your global, which is accessible everywhere anywhere, to the
nextMonth()
function. And function arguments basically declare a local variable, which is sets to the passed in value. You now have a globalmonth
variable set to1
, and a totally differentm
local to thenextMonth()
function also set to1
. You then increment the local variablem
to2
, which has no effect on the global variable.Instead, simply don't pass it in. Manipulate the global directly.
var month = new Date().getMonth() var nextMonth = function() { month++; setMonthSomewhere(month); }; nextMonth();
Now
nextMonth()
increments the globalmonth
variable directly, changing it's value.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
任何在函数中使用var被挂起。 后续对同一范围内的相同变量的使用不起作用。 它与a = 4;具有完全相同的含义a = 4; 单独。 Any use of var within a function is hoisted. Subsequent uses on the same variable in the same scope have no effect. It has exactly the same meaning as a = 4; alone.
-
Javascript Closure“为回调函数创建局部变量”(Javascript Closure “creating Local variable for the callback function”)[2023-11-06]
要为回调函数添加参数,您需要执行以下操作: datefield = $('fromdate'); datepicker = new DatePicker(wrapper, {months:1 , onSelect: (function(datefield) { return function() { onDatePick(); } }) (datefield ... -
是。 在希伯来语的日历中,有13个月的几年(准确的说是19年中有7次)。 Yes. In the hebrew calendar, there are several years with 13 months (7 out of 19 to be exact).
-
Javascript跟踪变量(Javascript Track Variable Change)[2023-09-07]
您可以使用显式方法设置值并将回调附加到该值。 这样做会有效: var Box = new Class({ setTitle: function(title) { this.title = title; // call your callbacks now. you probably need some way to register them btw } }); 调整想法以适合您的类语法... JS还有一个不太受支持的getter / setter语法(相 ... -
局部变量的偏移量作为常量“烘焙”到机器代码中。 编译器完成时,程序称为局部变量的内容将被编译器分配的固定内存偏移量替换。 假设您声明了三个局部变量: char a[8]; int b; short c; 编译器为这些变量分配偏移量: a位于偏移量0 , b位于偏移量8 , c位于偏移量12 。 假设您的代码执行b += c 。 编译器将其转换为如下所示的代码块: LOAD @(SP+8) ADD @(SP+12) STORE @(SP+8) 这里改变的唯一值是SP (堆栈指针)。 所有 ...
-
// the function creates a local scope. var refreshMediaAds = (function() { var mediaClickCounter = 0; // once executed it returns your actual function. return function _refreshMediaAds() { if (mediaClickCounter < 2) { medi ...
-
的JavaScript。(Javascript. Filter data)[2023-06-20]
你应该使用正则表达式 。 这将有助于您入门: var b = a.match( /DoB:\s*([^,]+),/)[1] 在DOB:\s*之后匹配一个或多个字符DOB:\s* - 换句话说, DOB:后跟0或更多的空格字符。 http://jsfiddle.net/w7d3x/ You should use a regex. This will help you get started: var b = a.match( /DoB:\s*([^,]+),/)[1] Matches one or mor ... -
创建局部变量以在javascript中跟踪日历上的月份。(Creating a local variable to keep track of months on a calendar in javascript.)[2022-07-27]
让我们尝试进一步削减这个钱位。 这基本上就是你在做什么。 var month = new Date().getMonth() var nextMonth = function(m) { m++; setMonthSomewhere(m); }; nextMonth(month); 所以会发生的事情是你将遍布任何地方的全局变量传递给nextMonth()函数。 函数参数基本上声明了一个局部变量,该变量设置为传入的值。 现在,您将全局month变量设置为1 ,并且nextMonth()函数的本地完 ...