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

New Installations created every time #180

Closed
dblythy opened this issue Jul 1, 2021 · 5 comments
Closed

New Installations created every time #180

dblythy opened this issue Jul 1, 2021 · 5 comments
Labels
type:question Support or code-level question

Comments

@dblythy
Copy link
Member

dblythy commented Jul 1, 2021

In the Android SDK, the device stores its installation. When .current is called again later, the same installation is saved.

I am saving Installation.current?, however each time the app is reopened a new Installation object is saved in the database.

Does anyone have any ideas?

@cbaker6
Copy link
Contributor

cbaker6 commented Jul 1, 2021

Linux and Android don’t have a Keychain. Are you initializing the Swift SDK with a key/ValueStore that you are persisting across launches? If not, there’s no way for the SDK store the previous installations or login. See comment: #98 (comment)

@cbaker6
Copy link
Contributor

cbaker6 commented Jul 1, 2021

I made the comments above because you mentioned "Android".

If you are using an Apple OS, can you show your code and explain what you are doing? For example, when do you first save your installation? Do you see the same issue when you signup and then save your installation in playgrounds? It seems the installation is behaving correctly there.

A proper flow to persist your installation:

  1. Signup/login
  2. Save installation

Also, did you look through #115 it may provide some insight

@cbaker6 cbaker6 added the type:question Support or code-level question label Jul 1, 2021
@dblythy
Copy link
Member Author

dblythy commented Jul 2, 2021

Hey @cbaker6, thank you for your response.

Installation is saved by:

func application(
      _ application: UIApplication,
      didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
        Installation.current?.setDeviceToken(deviceToken)
        Installation.current?.save { _ in }
    }

And on login:

Installation.current?.save { _ in }

And linked to user by:

Parse.Cloud.beforeSave(
  Parse.Installation,
  ({ user, object }) => {
    if (user) {
      object.set('user', user);
    } else {
      object.unset('user');
    }
    return object;
  },
  {
    skipWithMasterKey: true,
  }
);

Parse.Cloud.beforeLogin(({ installationId, object }) => {
  (async () => {
    const installation = await new Parse.Query(Parse.Installation)
      .equalTo('installationId', installationId)
      .first({ useMasterKey: true });
    if (installation) {
      installation.set('user', object);
      await installation.save(null, { useMasterKey: true });
    }
  })();
});

Parse.Cloud.afterLogout(({ installationId }) => {
  (async () => {
    const installation = await new Parse.Query(Parse.Installation)
      .equalTo('installationId', installationId)
      .first({ useMasterKey: true });
    if (installation) {
      installation.unset('user');
      await installation.save(null, { useMasterKey: true });
    }
  })();
});

The installation is functioning fine, it's just that every time a user logs in and out, a new installation is created rather than using the previous one. Should I just delete in installation object on logout?

@cbaker6
Copy link
Contributor

cbaker6 commented Jul 2, 2021

The installation is functioning fine, it's just that every time a user logs in and out, a new installation is created rather than using the previous one.

Yes, you are correct, a new installation is created on the client after logout due to #112.

Should I just delete in installation object on logout?

Yes, if you have a Cloud Code cleanup Installation function, you can use that if needed. If I remember correctly, the server deletes Installations on logout automatically

@cbaker6
Copy link
Contributor

cbaker6 commented Jul 10, 2021

Closing for now, feel free to reopen if there are more questions.

@cbaker6 cbaker6 closed this as completed Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Support or code-level question
Projects
None yet
Development

No branches or pull requests

2 participants