1

Attempting to keep track of an index for future use as an index in another container.

        uint64_t namehash = ::eosio::string_to_uint64_t(my_name.c_str());
        auto name_iter = names.find(namehash);

        uint64_t next_idx = (names.begin() == names.end() ? 0 : (name_iter)->id + 1 );

        if( name_iter == names.end() ){
            names.emplace(_self, [&](struct nameList &a){
                a.id = next_idx;
                a.name = my_name;
                a.hash = namehash;
            });
            name_iter = names.find(namehash);
         }

First one registers 0 for the a.id but the next series of registers that register seems to always be 1.

1

You can get the size of the multi_index and add one to it.

auto size = std::count(names.cbegin(), names.cend());
a.id = size + 1;
2
  • count will not work inside contracts...I used std::distance instead but it seems to be skipping over the 1 inside the table.
    – nakomis
    Mar 19 '19 at 16:57
  • What do you mean by "skipping over the 1 inside the table"? You have just one item in the table? Try adding more and test again.
    – Ami Heines
    Mar 19 '19 at 19:29
1

You can use the available_primary_key() function that belongs to multi index arrays:

 a.id = names.available_primary_key()

This gets an incrementally larger primary key than the previous highest key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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