0
// @dev An array that contains all the Cids of IPFS Content
string[] public cidsArray;

// Unique Cid to Content struct.
mapping(string => Content) public contentsMapping;


    function getTotalContentCount() public view returns (uint256) {
        return cidsArray.length;
    }

    /*  
    @dev
    If you have a public state variable of array type,
    then you can only retrieve single elements of the array via the generated getter function. 
    Therefore the entire array can only be returned by a function  
    */

    // @notice Function that returns entire cid array.
    function getCidArray() public view returns (string[] memory) {
        return cidsArray;
    }

So is it a good idea to get the whole array at once using that getCidArray() function, or is it better to get the array length and iterate through the generated getter function for the same array?

On the frontend ofcourse (next.js)

Which way is better and why?

It looks like both works, but what if the array contains a million elements?

1 Answer 1

1

Most RPC provider has a gas limit for eth_call t prevent spam and overloading of their infrastructure. Usually, it's about 2x-3x of the block gas limit. So as long as the gas of the view function is below your RPC providers limit, it should be fine.

If the array length won't reach a really high value, then it should be okay to fetch it all at once. But if the array can get arbitrarily large, it's a better idea to implement some sort of pagination. It's generally a good practice too as it makes sure things won't break in the future too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.