objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
918 stars 116 forks source link

Reordering list #584

Closed user97116 closed 4 months ago

user97116 commented 5 months ago

Hello, I have a class of task with subtasks, I want to swap two tasks from subtasks

from = parent.subtasks[a]; to = parent.subtasks[b]; temp = from from = to to = temp

this methods swap but objectbox return elements with ascending order of id and their associations won't change, I mean their subtasks are remains same

from = parent.subtasks[a]; to = parent.subtasks[b]; tempId = from.id from.id = to.id to.id = tempId

this methods swap ids but their associations won't change, I mean their subtasks are remains same

user97116 commented 5 months ago

Also this methods won't works

final from = all[startIndex];
final to = all[endIndex];
final id = from.id;
final fromMaintasks = [...from.maintasks];
from.id = to.id;
from.maintasks.clear();
from.maintasks.addAll(to.maintasks);
to.id = id;
to.maintasks.clear();
to.maintasks.addAll(fromMaintasks);
projectaApi.updateMany([all[startIndex], all[endIndex]]);
greenrobot-team commented 5 months ago

Could you provide working example code? I'm having a hard time to understand what exactly you want to do. I assume this is using relations? But then not? Please provide more info!

Some general remarks: there is no order when putting objects into a box or inside a relation. You can somewhat rely on the ID property, but I recommend to add a separate property for ordering and then use that when querying.

user97116 commented 4 months ago

@greenrobot-team I have list of entries in a list from objectbox, I want to reorder default list is order by id, suppose item id 1, item id 2, item id 3, then I want to swap id 1 to id 3 but it won't update subtasks that associiated with them item has subtasks,

greenrobot-team commented 4 months ago

Maybe this is the issue: you can't change the ID of an entity after it has been put. E.g. changing the ID below won't work. ObjectBox refers to objects by ID.

final from = all[startIndex];
final to = all[endIndex];
final id = from.id;
final fromMaintasks = [...from.maintasks];
from.id = to.id; // If the ID is changed here, ObjectBox will assume it should update 'to', not 'from'
from.maintasks.clear();
from.maintasks.addAll(to.maintasks);
to.id = id;
to.maintasks.clear();
to.maintasks.addAll(fromMaintasks);
projectaApi.updateMany([all[startIndex], all[endIndex]]);
github-actions[bot] commented 4 months ago

Without additional information, we are unfortunately not sure how to resolve this issue. Therefore this issue has been automatically closed. Feel free to comment with additional details and we can re-open this issue.