-2

I am calling a API which I am taking data from server. When The response have nextpageToken, The function call itself with pagetoken.

The code is working fine but it's make stackoverflow Exception. If this thing need to be called 350 times (method call itself everytime with new pagetoken) it's make stackoverflow exception.

for example this is the code.

public static void GetData(Channel info, string pagetoken =null)
{
  // get the data from api
    GetData(info,pagetoken)
  // enter into database
  return;
}

Someone please check how to figure this out. Previously I run some child thing in Task but it's failed because the API hits too fast so I remove that Task.

Now it's single threaded code and it's still not work perfectly.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
  • 1
    There is only one answer. Remove or at least reduce recursion. Do not call too many times The function from within the function. *(Your exapmple is a perfect example of recursion.)* – Julo Mar 24 '18 at 05:38
  • For example someone want data for last 60 days. it will send a lot of request and everytime it will call itself. please help, I am seeing this first time. – Anirudha Gupta Mar 24 '18 at 05:40
  • 1
    Based on what condition this recursion will end? I don't see any code related to that in your post? – Thangadurai Mar 24 '18 at 05:40
  • What about using counter and throw exception if counter exceeds maximum allowed recursion. – Dipen Shah Mar 24 '18 at 05:40
  • it appear that you entered into infinite loop. – rahulaga-msft Mar 24 '18 at 05:41
  • @Thangadurai If someone want data for last 7 day, it will end when data is older than 7 days. – Anirudha Gupta Mar 24 '18 at 05:41
  • 2
    @Adrian, I am not sure how and where you are doing that check, please post all relevant code. What I am saying is before calling GetData within the GetData funtion, you should check something like `if (pageToken !=null) GetData()` - The condition is just an example – Thangadurai Mar 24 '18 at 05:45
  • 1
    *Why* does the function need to call itself? Can't you just use a while-loop? – eddiewould Mar 24 '18 at 05:56
  • 1
    @Adrian there are two possible options. The condition that causes recursion is wrong *(continues even after the limit)*, or too many data *(too many recursions)*. You need to debug it step by step to find the problem. I personally do not see to you code or data, but recursion should be always used with care. There are *(at least sometime)* ways how to do things without recursion. Sorry I can not find now the example in C#, but at least there is some 'recursive file scanning' without recursion: https://stackoverflow.com/a/6776380/2826535 use some sort of 'stack of data to be get'. – Julo Mar 24 '18 at 05:58
  • @eddiewould because in the response I get the nextpagetoken and new request need to have nextpagetoken, – Anirudha Gupta Mar 24 '18 at 05:59
  • @Adrian you don't need recursion for that. Use a while loop (maybe do-while). – eddiewould Mar 24 '18 at 06:07
  • @eddiewould agreed, I am fixing it through do while loop – Anirudha Gupta Mar 24 '18 at 07:00

2 Answers2

4

Use a loop - sepearte your Method in two parts.. Make second method which does not handle the NextPage Token this new Method will only return the result.

in the "GetData" Method you do something like this

public static void GetData(Channel info, string pagetoken =null)
{
   // get the data from api
   var result = GetResultFromServer(info,pagetoken);        
   while(result != null)
   {
      //handle content of one page
      //
      // do something with one page.. add it to a result list or whatever 
      // you have to do
      // 
      if(result.nextPageToken != null)
      {
         var result = GetResultFromServer(info,result.nextPageToken )
       }
       else{result = null}
   }
   // enter into database
  return;
}
Manuel Amstutz
  • 1,311
  • 13
  • 33
3

Something like

string pageToken=null;
do { 
    pageToken = Getdata(info, pageToken):
} while (pageToken != null)
eddiewould
  • 1,555
  • 16
  • 36