strongloop-community / loopback-sdk-android

Android Client SDK for the LoopBack framework.
Other
67 stars 41 forks source link

How to check if an installation exists #46

Closed crandmck closed 10 years ago

crandmck commented 10 years ago

From https://groups.google.com/forum/#!topic/loopbackjs/Rz4kyjp_rq0/discussion.

Looking at the LocalInstallation class in the loopback-android repo, there is a save method but not really any others that interface with the loopback API AFAIK. I'm interested in a check exists method (/api/installations/{id}/exists). In particular, I'd like to have it so I can keep track of users who download an android app but haven't yet created an account.. That is, save installations before they can be associated with a userId (setting userId to -1 or something like that). To know whether or not to update the installation on the front screen (pre-login), setting the userId to -1, I need to know whether or not the installation exists already to avoid overwriting a user id that could still be associated with a user that is logged out or "Inactive".

    public void checkExists(final ObjectCallback callback) {
        ModelRepository<Model> repository =
                loopbackAdapter.createRepository("installation");
        final Model model = repository.createModel(
                BeanUtil.getProperties(this, false, false));      // I'm assuming this takes care of SharedPreferences id stuff..

        model.checkExists(new ObjectCallback() {      // checkExists would have to be written in Model.java
            @Override
            public void onSuccess() {
                result = model.exists();            // exists method would have to be written too.. 
                callback.onSuccess(boolean result);
            }

            @Override
            public void onError(Throwable t) {
                if (t instanceof HttpResponseException) {
                     // something graceful here.. 
                }
                callback.onError(t);
            }
        });
    }

If this is the way to go about it, happy to give it a shot. Wanted to check first before I dive in tho. Also wouldn't be very hard to write some Android code that just checks the server with a background task, however figured I'd try to use the loopback android library as much as possible to minimize code..

From Miroslav The current implementation of LocalInstallation class should already support your use case, at least in my opinion:

LocalInstance is storing the installationId in SharedPreferences, thus the save() method is always updating the same server instance of Installation model.

To know whether or not to update the installation on the front screen (pre-login), setting the userId to -1, I need to know whether or not the installation exists already to avoid overwriting a user id that could still be associated with a user that is logged out or "Inactive".

Every Installation instance is assigned a unique id bound the the copy of the application on a single device. No two devices share the same installation ids.

I don't understand why the "exists" check is needed to prevent overriding user ids. Could you please explain this in more detail? It's possible I am missing something here.

Perhaps what is needed is a better integration of LocalInstallation with authorization, so that LocalInstallation automatically fills the id of the user which is currently authenticated with the app? This should be IMO done on the server - when a local installation is create or updated, the userId should be overriden with the value from the access token.

Great call. By using null for userId at startup (not setting it at all, instead of setting to -1), eliminates need for exists call. Better solution, thanks.

Just to recap for anyone in future. Made two minor tweaks, but these steps are a solid way to do it as you suggested:

By doing it this way it covers every case I can think of:

There was one edge case where I had to add extra call: already logged in on new device (broken/lost phone recovery sort of thing, where old shared preferences make it to new device) -> update device token

crandmck commented 10 years ago

Need to prioritize.

bajtos commented 10 years ago

IMO, we should improve by doc by including this info:

By doing it this way it covers every case I can think of:

There was one edge case where I had to add extra call: already logged in on new device (broken/lost phone recovery sort of thing, where old shared preferences make it to new device) -> update device token

bajtos commented 10 years ago

Doc page: http://docs.strongloop.com/display/LB/Push+notifications+for+Android+apps#PushnotificationsforAndroidapps-CreateLocalInstallation

bajtos commented 10 years ago

Described in a new section: http://docs.strongloop.com/display/LB/Push+notifications+for+Android+apps#PushnotificationsforAndroidapps-WorkingwithLocalInstallationclass

@crandmck please review.

crandmck commented 10 years ago

I moved the new content to a separate article Working with the LocalInstallation class. Other than that, LGTM.