chennaione / sugar

Insanely easy way to work with Android Database.
http://satyan.github.com/sugar/
MIT License
2.62k stars 583 forks source link

When sending a SugarRecord as Serializable getId returns null #316

Open Elena0308 opened 9 years ago

Elena0308 commented 9 years ago

Hi, when extracting Serializable a SugarRecord object from extra in an intent object.getId() returns null.

ghost commented 9 years ago

Elena, you could implement Parcelable on your SugarRecord class. It's twice faster than serializing objects.

sureshjoshi commented 9 years ago

I would love to see the base SugarRecord as a Serializable object, as all it really contains from a field point of view is the ID... Doing so allows me to abstract my code, so that between activities, I'm passing an object, rather than needing a database call in each activity (this is more an encapsulation problem, not a performance problem for me).

In fact, the ONLY time I get burned by the serializable, is when I update my model - because it lost it's ID in Serialization, it creates a new object in the DB.

Parcelable is an option, but it has a lot of code overhead to handle (not only that, I have enums in my model - which are Serializable, but not Parcelable by default - without using ordinals and creating overhead there). The Parceler library would be great here, but it doesn't automatically wrap parent classes).

Currently, as a test hack, I've created an Ignore field which is backupId. I populate it when I have an ID, and then when I need to save my model, I call setId(backupId)... Bad hack/duplication - but actually significantly cleaner and less code than all the alternatives at hand (add about 3 lines of code + 1 every place you need to save a Serialized model - for me, this is an extra 5 lines of code in my app) :(

cutiko commented 7 years ago

Greetings, Im having the same problem, rather Serializable over Parcelable due the simple code, however the id is null

Tenkei commented 7 years ago

You should pass Object Id manually. Since Sugar do CRUD Operations base on defined Id field you should get/set the Id so sugar consider the passed object as same object. example in parcelable:

public MyObject(Parcel in){
    this.setId(in.readLong());
    ...
}

public void writeToParcel(Parcel parcel, int flag) {
    parcel.writeLong(this.getId());
    ...
}
WilvdMerwe commented 6 years ago

I have the same philosophy as @sureshjoshi. Even if there are better ways to handle this, it wasted a lot of my time as one of the big advantages of SugarRecord is the encapsulation as explained by @sureshjoshi and one just assumes it should work. Can this issue get attention please.

sureshjoshi commented 6 years ago

@WilvdMerwe Not sure if Sugar is still supported - I've moved on to Realm/Couchbase depending on my requirement - and am investigating Room (Android arch component).

Not sure how easy it is for you to migrate though - I keep my apps in a Clean architecture nowadays, so migrations are basically just creating a new database component and mappers (3-4 files) and I'm done

WilvdMerwe commented 6 years ago

@sureshjoshi Thanks for the recommendations. I received the project from a third party that used SugarRecord. Also received some recommendations for Room. Will probably have to migrate to that soon then.

sureshjoshi commented 6 years ago

@WilvdMerwe Ah. I know that pain well.

There might be Sugar forks which are kept more up to date maybe?

WilvdMerwe commented 6 years ago

@sureshjoshi Thanks for the recommendations. Migration shouldn't be too much of a problem. Searching for forks for a temporary fix isn't sustainable.