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

Support array types with Converter #42

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

Support array types with Converter #42

FabianTerhorst opened this issue Feb 27, 2017 · 21 comments
Labels
enhancement New feature or request

Comments

@FabianTerhorst
Copy link

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
Copy link
Member

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

@FabianTerhorst
Copy link
Author

FabianTerhorst commented Feb 28, 2017

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

@greenrobot
Copy link
Member

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

@greenrobot greenrobot changed the title Crash when query a entity that is using an converter Support array types with Converter Feb 28, 2017
@greenrobot greenrobot added the enhancement New feature or request label Feb 28, 2017
@FabianTerhorst
Copy link
Author

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
Copy link
Member

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

@FabianTerhorst
Copy link
Author

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

@FabianTerhorst
Copy link
Author

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
Copy link
Member

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

@FabianTerhorst
Copy link
Author

I could try to copy it into a sample add

@FabianTerhorst
Copy link
Author

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
Copy link
Member

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

@FabianTerhorst
Copy link
Author

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

@greenrobot
Copy link
Member

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

@FabianTerhorst
Copy link
Author

Also when the put call is inside an async transaction?

@greenrobot
Copy link
Member

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

@greenrobot
Copy link
Member

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

@matecode
Copy link

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
Copy link
Author

FabianTerhorst commented May 18, 2017

@matecode use List<String> instead.

@Drjacky
Copy link

Drjacky commented May 20, 2017

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

@greenrobot
Copy link
Member

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

@greenrobot-team
Copy link
Member

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
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants