Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation list order #1144

Open
stleusc opened this issue Aug 14, 2023 · 5 comments
Open

Relation list order #1144

stleusc opened this issue Aug 14, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@stleusc
Copy link

stleusc commented Aug 14, 2023

I have a problem that is pretty much similar to #243.

But I can add some details to it.
Let's say I have a class called Event that defines an event and the attendees (teams).

There is an overall list of all teams and each team can be in multiple events.
That is where the challenge comes in.

I need to be able to manually 'sort'/rearrange the teams in each event. The order matters.
I am struggling to store the 'sortorder' anywhere. I can not add it to the team class since the identical team can be in several events and with that will have a unique position in each event. That would leave the event class as the logical place.
But I am not seeing a good way to do that either.

Does ObjectBox support a solution for that? Basically maintaining the sortorder of the ToMany in ANY way?

@Entity
public class Event {
    @Id public long id;
    public String name;
    public ToMany<Team> teams;
}

@Entity
public class Team {
    @Id public long id;
    public String name;
    ...
}
@stleusc stleusc added the bug Something isn't working label Aug 14, 2023
@greenrobot-team
Copy link
Member

There are proposed changes and a request for input in #243 (comment)

We welcome any feedback or other suggestions.

@greenrobot-team greenrobot-team added enhancement New feature or request and removed bug Something isn't working labels Aug 21, 2023
@ajans
Copy link

ajans commented Oct 12, 2023

You can add a list of long-IDs to the Event-entity and persist those with a converter:

    @Convert(converter = ListLongConverter.class, dbType = String.class)
    public List<Long> teamsOrder;

The converter is responsible for converting the List-type to something you can persist in ObjectBox.
See https://docs.objectbox.io/advanced/custom-types#convert-annotation-and-property-converter

For ease of use, I post some kotlin-code here of how I use it with Gson for such a case, since I already have Gson in my project, but maybe just for this, you should simply convert the list to a string of comma-separated values and vice versa:

class ListLongConverter : PropertyConverter<List<Long>?, String?> {
    private val gson = Gson()
    private val listLongType: Type = object : TypeToken<List<Long>?>() {}.type

    override fun convertToEntityProperty(databaseValue: String?): List<Long>? {
        return gson.fromJson(databaseValue, listLongType)
    }

    override fun convertToDatabaseValue(entityProperty: List<Long>?): String? {
        return gson.toJson(entityProperty)
    }
}

And last but not least, you can then set a comparator on the ToMany-field to tell it to sort its content according to the order of the teamsOrder-list (again kotlin-code, but you may get the idea):

    event.teams.setComparator { team1, team2 ->
        // something like that, did not test
        val team1OrderIndex = event.teamsOrder.indexOf(team1.id)
        val team2OrderIndex = event.teamsOrder.indexOf(team2.id)
        sign(team1OrderIndex - team2OrderIndex).toInt()
    }

See https://objectbox.io/docfiles/java/current/io/objectbox/relation/ToMany.html#setComparator(java.util.Comparator) for details

@jy-98

This comment was marked as off-topic.

@jy-98

This comment was marked as off-topic.

@greenrobot-team

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants