Is there a way to batch saving of objects to a LocalDB

For instance, if this function is being called (say) 600 times:

    saveIndividualDisplayFilter: function(description, number, filterText, dropdownIdentifier) {
        var display_filter = LocalDB.display_text_animal.create();
        display_filter.description = description + " ("+number+")";
        display_filter.filter_text = filterText;
        display_filter.dropdown_identifier = dropdownIdentifier;
        display_filter.save();
    },

can the saving part be made quicker?
Or is there a way to display unsaved objects in a object-dropdown ui element?

Hi @chansmann

You can use LocalDB.Batch() - Documentation shows example of DB.Batch Batch Operations (App) - JourneyApps Docs

You can use LocalDB or OnlineDB or just DB.
Example:

// JS
var batch = new LocalDB.Batch();
for(var i = 0; i < 200; i++) {
    var item = LocalDB.item.create({message: 'Item ' + i});
    batch.save(item);
}
batch.execute();

That is one way, but note that you do not have to save these objects if you are wanting to use them once-of. You can push the objects onto an array and then reference the array in the xml

// JS
var item_array = []
for(var i = 0; i < 200; i++) {
    var item = LocalDB.item.create({message: 'Item ' + i});
    item_array.push(item);
}
// XML 
<var name="items" type="array:item" />
<var name="selected_item" type="item" />

<object-dropdown query="items" bind="selected_item" empty-message="Your items will appear here" required="false" />

Thanks Willem!

Hi

I am using the array/objects only once-off.
How do I prevent the array from being auto-saved when I move to a new activity/view?

Is it enough to clear all in-memory references to the array? Even though LocalDB.item.create was called on every item?

(I am assuming that auto-saving is my problem)

I see that:
return navigate.discard().link(“next_screen”);
and just using discard() instead of dismiss()
seems to make a difference

@chansmann when using discard() or the built-in back button (top left hand corner), all unsaved changes to view variables will be discarded. Conversely, when using any other navigation (link, clear, replace, even dismiss, etc.) without discard, the current state of the view variables will be saved.

So if you want to avoid the auto-save, just start your navigation with a navigate.discard() and then chain the rest of the navigation, i.e. navigate.discard().link('some_path') or navigate.discard().replace('some_path')

Thanks Tielman!