伪代码,因为我们没有具体的例子有工作,但...
大多数API使用分页将有项目的总数。您可以设置每个迭代的最大项目并像这样跟踪它,或者检查null next_object,具体取决于API如何处理它。
List<ApiObject> GetObjects() {
const int ITERATION_COUNT = 100;
int objectsCount = GetAPICount();
var ApiObjects = new List<ApiObject>();
for (int i = 0; i < objectsCount; i+= ITERATION_COUNT) {
// get the next 100
var apiObjects = callToAPI(i, ITERATION_COUNT); // pass the current offset, request the max per call
ApiObjects.AddRange(apiObjects);
} // this loop will stop after you've reached objectsCount, so you should have all
return ApiObjects;
}
// alternatively:
List<ApiObject> GetObjects() {
var nextObject = null;
var ApiObjects = new List<ApiObject>();
// get the first batch
var apiObjects = callToAPI(null);
ApiObjects.AddRange(apiObjects);
nextObject = callResponse.nextObject;
// and continue to loop until there's none left
while (nextObject != null) {
var apiObjects = callToAPI(null);
ApiObjects.AddRange(apiObjects);
nextObject = callResponse.nextObject;
}
return apiObjects;
}
这无论如何是基本的理念,每两个常用的Web服务方法(有很多细节的冷落,因为这是不工作的代码,但只是演示的一般方法)。
为什么不使用类的属性来保留它?真的不知道问题是什么,如果你已经有了所有的变量,为什么不保留对它们的引用呢? – Arjang
是的,但如何反思相同的方法? – Bokbob
你能复制你使用的代码吗? @Bokbob – MoustafaS