4

I am trying to create a endless table in Xamarin as a Instagram Client. However when I scroll to the bottom of the screen it loads the same 2nd page of images back. It loads the first page normally then it loads the 2nd page normally, however when i try to load the 3rd page it loads the 2nd page again. How would i Fix this? This is how I am attempting to load it:

Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();

var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);

var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, response => {
    var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);

    string nextURL = rootObject.pagination.next_url;
    //// Create Next Client
    var requestBack = new RestRequest ();
    var clientBack = new RestClient (nextURL);

    // GET Next Response
    clientBack.ExecuteAsync (requestBack, responseBack => {
        var rootObjectBack = JsonConvert.DeserializeObject<RootObject> (responseBack.Content);
        table.InvokeOnMainThread (() => {
            // Create list that contains newly attained JSON
            List<Datum> instagramData = rootObjectBack.data;
            // Create dummy list for other values
            List<Datum> sloppy = null;
            ((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
            table.ReloadData ();
            });
        });
    });

This is where I detect that the user is at the bottom of the TableView

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {

        NetworkCheck netCheck = new NetworkCheck ();
        netCheck.runCheck ();
        bool log = netCheck.anyLoggedIn ();

        if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
            // this is the last row in the last section
            Console.WriteLine ("~~~Bottom~~~");
            FLDTRequest fldt = new FLDTRequest ();
            fldt.getPastInstagramFeed (tableView);
        }
        var cell = tableView.DequeueReusableCell(InstagramCell.Key) as InstagramCell;

        if (cell == null)
        {
            cell = new InstagramCell();
            var views = NSBundle.MainBundle.LoadNib("InstagramCell", cell, null);
            cell = Runtime.GetNSObject(views.ValueAt(0)) as InstagramCell;
        }

        Datum h = instaData [indexPath.Row];
        cell.BindData(h);

        return cell;
    }
Prad
  • 469
  • 1
  • 8
  • 28
  • I don't see any code to the load the third page. You appear to be loading one page, then using nextURL to get the next page, but I don't see you doing a third call using the 2nd page's value of nextURL – Jason Jun 21 '14 at 23:26
  • Oh i see could you help me create a function that would load the 3rd page and so on. Your help would be greatly appreciated. – Prad Jun 22 '14 at 04:03
  • it appears the everything from the "create new client" comment to the end is essentially what you need to grab the "next" set of data, you just need to pass in the new "nextUrl" each time – Jason Jun 22 '14 at 13:08
  • @Jason could you help me edit my method to do that? – Prad Jun 23 '14 at 01:22

1 Answers1

1

Put the callback in a seperate recursive function that keeps calling itself as long as there is a nexturl. Don't know the instagram API but something like this should work.

Run this in viewdidload or something (NOT every time you reach bottom but once)

nextUrl = null;

Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();

var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);

var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, resp =>
    {
        PageRetrievedCallback(resp );
    });

Callback function

store nextUrl as class value

string nextUrl = null;

public void PageRetrievedCallback(RestResponse response)
{
    var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
    nextURL = rootObject.pagination.next_url;

    table.InvokeOnMainThread (() => {
            // Create list that contains newly attained JSON
            List<Datum> instagramData = rootObject.data;
            // Create dummy list for other values
            List<Datum> sloppy = null;
            ((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
            table.ReloadData ();
         });
}

run this when you detect bottom to load next page:

if ( ! string.IsNullOrEmpty (nextURL) ) 
{

    var requestBack = new RestRequest ();
    var clientBack = new RestClient (nextURL);
    clientBack.ExecuteAsync (requestBack, resp =>
    {
        PageRetrievedCallback(resp );
    });
}
svn
  • 1,235
  • 10
  • 22
  • When I call the the function from the top code at `client.ExecuteAsync (request, PageRetrievedCallback);` it gives me an error saying that I need to put a parameter for the `RestResponse response` parameter of the `PageRetrievedCallback(RestResponse response)` – Prad Jun 25 '14 at 16:40
  • it shows all the posts but when i scroll to the bottom again it shows the latest post once again – Prad Jun 26 '14 at 14:06
  • When you drag to the bottom of the screen all the way on the bottom of the table view the code is fired – Prad Jun 26 '14 at 15:06
  • I edited my question to Include the function that I use to call the nextPage function – Prad Jun 26 '14 at 15:09
  • You were loading the complete feed every time you hit bottom. So ofcourse the first will show up again. I've updated my anwser again. please read carfully – svn Jun 27 '14 at 08:46
  • The function that detects whether or not the user is on the bottom of the screen, and the function that gets the PastInstagramPictures are in to completely different classes and so what you have suggested did not work... – Prad Jun 27 '14 at 16:33
  • Your question is changing every time I update my answer. I gave you all the pieces. How you should put that together depends on your code. If you can't figure it out post a new clear question with ALL relevant code. – svn Jun 30 '14 at 08:23
  • You are probably running it to early. But again I can't tell without seeing all the code – svn Jul 03 '14 at 08:55