How can I savely iterate over a foundset and delete some of the records?
I read about the foundset updater. But all I found is, that I can change attribute values. Can I also delete a record and continue iterating over the foundset updater? Or will I get problems with the index counter?
I don’t know about the FoundSetUpdater but if you use a foundset it will give you problems because the record will be removed from the foundset the moment you delete it. One solution for this is to loop back in the foundset.
for ( var i = databaseManager.getFoundSetCount(foundset) ; i > 0 ; i--)
{
// do your thing
}
Thank you for the answer. Looping backwards is what I do now. It helps me that you suggest it.
I thought there might be a more advanced way. The name foundset updater was leading me to the idea, it might offer a stable iteration. Actually, I do not understand what this foundset updater is for. I sometimes iterate over a normal foundset, update values and save the data. Is this wrong?
Be careful here, when the foundset is 500 records, this won’t work!
getRecord(500) will not return the record if the chunk the record is in has not been loaded yet.
If the delete-condition can be expressed in search conditions you could use find, search and deleteAllRecords().
If there is more complex logic for the selection of records-to-be-deleted i would suggest something like:
for (i = 1; i < foundset.getSize(); i++)
{
if (mydeletecheck(foundset.getRecord(i)))
{
if (foundset.deleteRecord(i))
{
i--; // record is removed from the set, all indexes are shifted by 1
}
}
}
The foundsetupdater is designed to perform updates on large foundsets in 1 update statement in the database which performs better than looping.
birgit:
I’ll have to check if I might hit the 500 border
500 was just an example number, the real border is defined by chunks of 200 Servoy fetches by.
getRecord(1) will load pks 1-200 (the first chunk)
getRecord(201) will load pks 201-400 (the second chunk) when the first chunk is loaded.
But getRecord(500) will only load pks 401-600 (third chunk) when the second chunk is loaded.
So if you start with a fresh foundset and call getRecord(500) out of the blue, nothing will be loaded.
What if you do a getRecord(500) and then a controller.setSelectedIndex(500)? Will that load all PK’s ?
I use this technique to jump to the last record of a foundset even when the PK’s aren’t loaded yet.
I think my previous posts were not entirely correct, it appears that a foundset.getRecord(500) out of the blue does load pks 1-600 even if nothing is loaded yet.
That is what one gets when one starts blabbing on the forum without checking first
controller.setSelectedIndex(idx) does only work if the record was loaded, I guess i was mixing those 2 up.