0

Before i explain anything about mu question i want to put my code so that i can refer it

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract GeoSpatialData{

    uint public SpatialDataCount = 0;
    
    struct GeoData{
        uint id;
        address owner_address;
        string data_name;
        string data_year;
        string data_description;
        string data_img_url;
        string data_url;
        string data_category;
        string data_published_date;
    }

    GeoData[] public geoDatas;

    mapping (uint => GeoData) public geodatas;
    // write function
    function createData(string memory _data_name,address _owner_address,string memory _data_year,string memory  _data_description,string memory _data_image_url,string memory _data_url,string memory _data_published_date,string memory _data_category,uint _id ) public{
        SpatialDataCount++;
        uint used_id = SpatialDataCount+_id;
        geoDatas.push(GeoData({id: used_id,owner_address:_owner_address,data_name:_data_name,data_year:_data_year,data_description:_data_description,data_img_url:_data_image_url,data_url:_data_url,data_category:_data_category,data_published_date:_data_published_date}));
        // geoDatas.push(Ge)
    }
    // read all data  function
    function readData() public{

    }

    // read single data function 
    function readSingle() public{

    }

}

I want to create a function to read single struct data using the id function which i make to be unique always and then i also want to create a function by which i can read the whole struct as of now later i want to have a pagination functionality how can i achive this

1 Answer 1

1

You cannot read unlimited amount of data from the chain via a single function, so getting all of the structs is not possible. Not unless you modify your own node and allow eth_call to be able to use unlimited gas.

If you need paginated access and your own id is not ordered, you can do something like:

GeoData[] public geoData;
mapping(uint => uint) idIndexMap; // mapping your id to array index

function getGeoData(uint256 id) public view returns (GeoData memory) {
  uint256 index = idIndexMap[id];
  return geoData[index];
}

function paginatedGetGeoData(uint256 offset, uint256 count) public view returns (GeoData[] memory) {
  GeoData[] memory data = new GeoData[](count);
  for (uint256 i = 0; i < count; i++) {
    data[i] = geoData[offset + i];
  }
  return data;
}

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.