3

ActiveAndroid:

I need to push updates to specific table fields without wiping out any existing data in the row if i dont happen to save() data for all fields each time.

For example:

I have a table named 'user' which contains the following fields:

extSrc | extId | email | firstName | lastName | role | photo | userId | verified

If i save data to all fields except 'verified' and then I later need to update only the verified field - is this possible?

Currently when i use the code below, all fields except userId (which i have stored locally) and verified are populated, all other fields are cleared. At the time that i have the verified value that i need to update i dont have the additional user data to push / save() so i only need to update the specific 'verified' field when the userId matches a userId in the user table and leave all other fields for that user as they are.

My code:

Model:

    // define table name
    @Table(name = "User")

    public class User extends Model 
    {
    // define table columns

    @Column(name = "extSrc")
    public String extSrc;

    @Column(name = "extId")
    public String extId;

    @Column(name = "email")
    public String email;

    @Column(name = "firstName")
    public String firstName;

    @Column(name = "lastName")
    public String lastName;

    @Column(name = "role")
    public String role;

    @Column(name = "photo")
    public String photo;

    @Column(name = "userId", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public String userId;

    @Column(name = "verified")
    public String verified;
    }


controller:


    public class UserAdapter implements JsonDeserializer {

    @Override
    public User deserialize(JsonElement arg0, Type arg1,
        JsonDeserializationContext arg2) throws JsonParseException {

            User u = new User();

            Log.v("user", u.toString());

            JsonObject j = (JsonObject) arg0;

            Log.v("j", j.toString());


            if(j.has("extSrc"))
            {
                u.extSrc = j.get("extSrc").getAsString();
                Log.v("extSrc", u.extSrc);
            }
            else
            {
                Log.v("extSrc", "does not exist");
            }


            if(j.has("extId"))
            {
                u.extId = j.get("extId").getAsString();
                Log.v("extId", u.extId);
            }
            else
            {
                Log.v("extId", "does not exist");
            }


            if(j.has("email"))
            {
                u.email = j.get("email").getAsString();
                Log.v("email", u.email);
            }
            else
            {
                Log.v("email", "does not exist");
            }


            if(j.has("firstName"))
            {
                u.firstName = j.get("firstName").getAsString();
                Log.v("firstName", u.firstName);
            }
            else
            {
                Log.v("firstName", "does not exist");
            }


            if(j.has("lastName"))
            {
                u.lastName = j.get("lastName").getAsString();
                Log.v("lastName", u.lastName);
            }
            else
            {
                Log.v("lastName", "does not exist");
            }


            if(j.has("role"))
            {
                u.role = j.get("role").getAsString();
                Log.v("role", u.role);
            }
            else
            {
                Log.v("role", "does not exist");
            }


            if(j.has("photo"))
            {
                u.photo = j.get("photo").getAsString();
                Log.v("photo", u.photo);
            }
            else
            {
                Log.v("photo", "does not exist");
            }


            if(j.has("userId"))
            {
                u.userId = j.get("userId").getAsString();
                Log.v("userId", u.userId);
            }
            else
            {
                Log.v("userId", "does not exist");
            }


            if(j.has("verified"))
            {
                u.userId = SpontlyApplication.PREFS_USER_ID;
                u.verified = j.get("verified").getAsString();

                Log.v("verified", u.verified);
            }
            else
            {
                u.verified = "true";
                Log.v("verified", "does not exist");
            }

     u.save();

     }   

Thanks.

user2252028
  • 31
  • 1
  • 3

1 Answers1

2

If you want to update already existing user in your database, you need to retrieve it first from the database and update its fields before calling save();

Something like:

User user = new Select().from(User.class).where("userId = ?", userIdYouWantToRetrieve).executeSingle();

if (user != null){
   user.setVerified(true);
} else {
   user = new User(){//populate your new user here from json }
}

user.save();

This will keep the users values as were previously stored in the database and you will only update the new values you want.

MikeL
  • 5,385
  • 42
  • 41
  • Very important note: Remember that there is a caching in ActiveAndroid (unless you alter the code to disable the caching or clear cache) so if in some part of the code you retrieve a user and change its field without intention to save the data and in another code section you retrieve the user again, you will get the cached one with the changes you did. Saving user at this point will save all the previous changes and the new change you did. – MikeL Oct 31 '16 at 07:51