13 Jan 2012

The Twitter api: the max_id timeline param

I have been making some improvements to Twhii of late. Memory management, better sign-on process and all that. Among the numerous others, one thing that has come with the update is adding a 'Load More' button to load more tweets.

The 'load more' functions differently from loading new tweets. Actually, this loads previous tweets. Loading new tweets uses the since_id parameter in related timeline APIs while loading previous tweets uses the max_id parameter (see https://dev.twitter.com/docs/api/1/get/statuses/home_timeline for sample doc). In other words, if you have a stream of tweets, you can take the id of the topmost tweet (i.e the most recent) and pass it as a since_id parameter to fetch recent tweets (i.e new tweets after that id). You can also take the id of the last tweet and pass it as max_id parameter to fetch tweets before that id. However, max_id (as documented and tested) may include the tweet with the passed id in the returned stream.

I don't know Twitter's reason for this but I think it shouldn't be so. If I want to get tweets before 'tweetA' and so passed the id of tweetA to the API, I shouldn't get tweetA as part of the returned array of tweets. Even since_id doesn't work this way.

My simple workaround for now is to reduce the id by 1 before passing to the API.

1
2
3
4
String id = jo.getString("id_str"); 
long idL = Long.parseLong(id); 
idL -= 1; 
params.put("max_id", idL+""); 

My name is Opeyemi Obembe. I build things for web and mobile. You should follow me on Twitter (@kehers).