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

Support array types with Converter #42

Closed
FabianTerhorst opened this Issue Feb 27, 2017 · 21 comments

Comments

Projects
None yet
5 participants
@FabianTerhorst

FabianTerhorst commented Feb 27, 2017

Im getting the following error when im query the entity that is using an converter:

 io.objectbox.exception.DbException: Entity is expected to have this constructor: Group(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;ZZZZL[J;)V

The constructor:

public Group(long id, String ..., String ..., String ..., boolean ..., String ...,
            boolean ..., boolean ..., boolean ..., boolean ..., long[] deviceIds)

The Entity converter usage:

@Convert(converter = DeviceIdConverter.class, dbType = String.class)
private long[] deviceIds;

The Converter Class:

public class DeviceIdConverter implements PropertyConverter<long[], String> {

    private static final ByteString SEMICOLON = ByteString.decodeBase64(";");

    @Override
    public long[] convertToEntityProperty(String s) {
        if (s == null) {
            return new long[0];
        }
        try {
            long[] ids = new long[0];
            Buffer buffer = new Buffer();
            buffer.writeUtf8(s);
            long index;
            while ((index = buffer.indexOf(SEMICOLON)) != -1) {
                ids = add(Long.parseLong(buffer.readUtf8(index - 1)), ids);
            }
            return ids;
        } catch (IOException io) {
            io.printStackTrace();
            return new long[0];
        }
    }

    public long[] add(long id, long[] values) {
        long[] anotherArray = new long[values.length + 1];
        System.arraycopy(values, 0, anotherArray, 0, values.length);
        anotherArray[values.length] = id;
        return anotherArray;
    }

    @Override
    public String convertToDatabaseValue(long[] integers) {
        Buffer buffer = new Buffer();
        for (long value : integers) {
            buffer.write(SEMICOLON);
            buffer.writeUtf8(String.valueOf(value));
        }
        return buffer.toString();
    }
}
@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Feb 27, 2017

Member

Never tested converters with array types. Could you check if List is working?

Member

greenrobot commented Feb 27, 2017

Never tested converters with array types. Could you check if List is working?

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Feb 28, 2017

I have tested it now with List<Long> and its working.

FabianTerhorst commented Feb 28, 2017

I have tested it now with List<Long> and its working.

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Feb 28, 2017

Member

Great, I will rename the ticket accordingly to support array types.

Member

greenrobot commented Feb 28, 2017

Great, I will rename the ticket accordingly to support array types.

@greenrobot greenrobot changed the title from Crash when query a entity that is using an converter to Support array types with Converter Feb 28, 2017

@greenrobot greenrobot added the feature label Feb 28, 2017

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 5, 2017

It looks like even Lists aren´t working as well. Immediately after data is available (list is not null) and the object is getting inserted to the database, the database won´t return any entities anymore in any box.

It looks like even Lists aren´t working as well. Immediately after data is available (list is not null) and the object is getting inserted to the database, the database won´t return any entities anymore in any box.

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 5, 2017

Member

@FabianTerhorst Can you investigate a bit? Is it really related to converters? If not, please open a new ticket.

Member

greenrobot commented Apr 5, 2017

@FabianTerhorst Can you investigate a bit? Is it really related to converters? If not, please open a new ticket.

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 5, 2017

It is. When i remove the converter annotation the app is working again.

It is. When i remove the converter annotation the app is working again.

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 5, 2017

It is also not depending on the converter. Even when im returning a new arraylist every time the bug occur.

FabianTerhorst commented Apr 5, 2017

It is also not depending on the converter. Even when im returning a new arraylist every time the bug occur.

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 7, 2017

Member

@FabianTerhorst You don't have a reproducible test case you can share, I guess?

Member

greenrobot commented Apr 7, 2017

@FabianTerhorst You don't have a reproducible test case you can share, I guess?

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 7, 2017

I could try to copy it into a sample add

I could try to copy it into a sample add

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 10, 2017

While creating the converter inside the sample project i have noticed that the converter was throwing an exception that wasn´t thrown in the main app. I fixed the converter, copied the fix into the main app and everything was working how expected.

While creating the converter inside the sample project i have noticed that the converter was throwing an exception that wasn´t thrown in the main app. I fixed the converter, copied the fix into the main app and everything was working how expected.

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 10, 2017

Member

@FabianTerhorst Alright. Was the exception swallowed, so you did not notice in the first place?

Member

greenrobot commented Apr 10, 2017

@FabianTerhorst Alright. Was the exception swallowed, so you did not notice in the first place?

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 10, 2017

@greenrobot yes, there was no strack trace inside the main app, just the database was not working correctly.

@greenrobot yes, there was no strack trace inside the main app, just the database was not working correctly.

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 10, 2017

Member

@FabianTerhorst I just checked: we have unit tests to ensure exceptions from the converter are thrown from both directions (put and get).

Member

greenrobot commented Apr 10, 2017

@FabianTerhorst I just checked: we have unit tests to ensure exceptions from the converter are thrown from both directions (put and get).

@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst Apr 10, 2017

Also when the put call is inside an async transaction?

Also when the put call is inside an async transaction?

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 10, 2017

Member

In that case TxCallback must be called with an exception. Did that happen?

Member

greenrobot commented Apr 10, 2017

In that case TxCallback must be called with an exception. Did that happen?

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Apr 10, 2017

Member

P.S.: Added a unit test for that constellation. Works as expected.

Member

greenrobot commented Apr 10, 2017

P.S.: Added a unit test for that constellation. Works as expected.

@matecode

This comment has been minimized.

Show comment
Hide comment
@matecode

matecode May 18, 2017

I fixed the converter, copied the fix into the main app and everything was working how expected.

@FabianTerhorst How did you fixed that? I have the same issue with a String Array

This is my converter

public static class ServicesConverter implements PropertyConverter<String[], String> {

        @Override
        public String[] convertToEntityProperty(String s) {
            if (TextUtils.isEmpty(s))
                return null;
            else return s.split(",");
        }

        @Override
        public String convertToDatabaseValue(String[] strings) {
            if (strings != null && strings.length > 0) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < strings.length; i++) {
                    if (i > 0) builder.append(",");
                    builder.append(strings[i]);
                }
                return builder.toString();
            }
            return null;
        }
    }

matecode commented May 18, 2017

I fixed the converter, copied the fix into the main app and everything was working how expected.

@FabianTerhorst How did you fixed that? I have the same issue with a String Array

This is my converter

public static class ServicesConverter implements PropertyConverter<String[], String> {

        @Override
        public String[] convertToEntityProperty(String s) {
            if (TextUtils.isEmpty(s))
                return null;
            else return s.split(",");
        }

        @Override
        public String convertToDatabaseValue(String[] strings) {
            if (strings != null && strings.length > 0) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < strings.length; i++) {
                    if (i > 0) builder.append(",");
                    builder.append(strings[i]);
                }
                return builder.toString();
            }
            return null;
        }
    }
@FabianTerhorst

This comment has been minimized.

Show comment
Hide comment
@FabianTerhorst

FabianTerhorst May 18, 2017

@matecode use List<String> instead.

FabianTerhorst commented May 18, 2017

@matecode use List<String> instead.

@Drjacky

This comment has been minimized.

Show comment
Hide comment
@Drjacky

Drjacky May 20, 2017

@FabianTerhorst I couldn't figure out. Please look at this question:
http://stackoverflow.com/questions/44082952

Drjacky commented May 20, 2017

@FabianTerhorst I couldn't figure out. Please look at this question:
http://stackoverflow.com/questions/44082952

@greenrobot

This comment has been minimized.

Show comment
Hide comment
@greenrobot

greenrobot Jul 21, 2017

Member

Does anyone have issues with List types in 0.9.13? If so, please open a separate issue. Thanks!

Member

greenrobot commented Jul 21, 2017

Does anyone have issues with List types in 0.9.13? If so, please open a separate issue. Thanks!

@greenrobot-team

This comment has been minimized.

Show comment
Hide comment
@greenrobot-team

greenrobot-team Jul 17, 2018

Collaborator

Tested using long[] and works as expected (ObjectBox 1.5.0 and 2.0.0-beta).

The easiest way to work around constructor-related DbException when getting/querying entities is to make sure to provide a no-args constructor.

Please submit a new issue if further issues become known.
-ut

Collaborator

greenrobot-team commented Jul 17, 2018

Tested using long[] and works as expected (ObjectBox 1.5.0 and 2.0.0-beta).

The easiest way to work around constructor-related DbException when getting/querying entities is to make sure to provide a no-args constructor.

Please submit a new issue if further issues become known.
-ut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment