사이비 코드는 페이지 매김와
대부분의 API는 항목의 총 수있을 것이다. 반복마다 최대 항목을 설정하고이를 추적하거나 API가 처리하는 방법에 따라 null next_object를 확인할 수 있습니다. 두 개의 일반적인 웹 서비스 방식에 따라, 어쨌든 기본적인 생각이다
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;
}
(이 코드를 작동하지만 일반적인 접근 방식을 보여주기 위해 의미되지 않는 세부의 많은이, 왼쪽으로).
클래스의 속성을 사용하여 유지하지 않는 이유는 무엇입니까? 정말로 문제가 뭔지 잘 모르겠다. 만약 당신이 이미 모든 변수를 가지고 있다면 왜 그 변수들을 참조하지 않는가? – Arjang
그래,하지만 같은 방법을 refire하는 방법? – Bokbob
사용한 코드를 복사 할 수 있습니까? @ 복본 – MoustafaS