Monday, August 24, 2015

iOS Box API content fetch limit using Objective c

This article is for iOS developers who are using BOX API in their application. As this is for BOX API users, Here I am not discussing about what is BOX and what it's API gives us. Directly I want to discuss about, How to play with the content limit option in the BOX API.

Here I want to tell like why I am writing this article for such a simple thing. It's look like that :). I used BOX API in one of my applications, where client will setup all his content (folders & files) in the BOX and my iOS app has to fetch the content and has to able to play with the content like upload, download, rename, creation of folders etc.

There were around 120 folders in the BOX that my client has setup. He has perfectly organised those for demo purpose. I have developed this application with a sample BOX account which has around 20 folders like that. I am fetching content using the BOX API's BoxFoldersRequestBuilder and foldersManager classes. I used below code to fetch root folder content from the BOX using BOX API classes. 

BoxFoldersRequestBuilder *folderReqBuilder = [[BoxFoldersRequestBuilder alloc]      
                     initWithQueryStringParameters:@{ @"fields" : @"name,type,
[[BoxSDK sharedSDK].foldersManager folderItemsWithID:folderID 
                     requestBuilder: folderReqBuilder success:success failure:failure];

As I had only 20 folders in the BOX development account, the above service call to BOX gives me all the 20 folders metadata (name, description etc...). So, I am good for development. But the actual mistake is I didn't check for the default number of assets metadata that BOX gives. Obviously If I have around 1000 items in my BOX, it is not supposed to give me all the assets metadata. 

This content limit option came into picture after sending the application to client. I have used UICollectionView to display the content(files & folders) to the app users. As my client has around 120 assets, he is not able to see all the 120 assets in the UICollectionView. Everytime he is able to see only the first 100 assets. 

We though, it's a rendering issue of the UICollectionView. But it's not. The issue here is by default BOX gives only first 100 assets. So, If we want more content, we need to play with the 'limit' option at the time of requesting the metadata of the root folder content.

For this, I have added limit option in the BoxFoldersRequestBuilder of BOX API as shown below.

BoxFoldersRequestBuilder *folderReqBuilder = [[BoxFoldersRequestBuilder alloc
                     initWithQueryStringParameters:@{ @"fields" : @"name,type,
[[BoxSDK sharedSDK].foldersManager folderItemsWithID:folderID 
                     requestBuilder: folderReqBuilder success:success failure:failure];

I have added 'limit' key and 150 as the value in the 'initWithQueryStringParameters' of the folderRequestBuilder. That means, I am requesting for the first 150 assets instead of the default 100 assets limit. My request for 150 assets is because I know that my client is having around 150 assets in the root folder. So, the limit value should be based on the number of assets that we want to display. If there are more number of assets, we can go with the pagination option.

Hope this article is useful to iOS BOX API users.
Feel free to comment in case of any queries/concerns.