# Cache issue on repositories when changing apiKey

**URL:** <https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333>\
**Category:** Android\
**Created:** [October 19, 2021, 12:55am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333 "2021-10-19T00:55:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![franco](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@franco](https://forum.social.plus/u/franco)\
**Post date:** [October 19, 2021, 12:55am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333/1 "2021-10-19T00:55:44Z")

</div>

Our App uses 2 Amity configs (both in US region, but we have 2 keys), and we have a set of users that will be able to jump between the two of them. They do so by login in and out with different user ids

So, depending on their region we will initialize the AmityCoreClient.setup with the corresponding key. And also, call unregisterDevice when they log out.

We also make use of CommunityRepository. Which is caching the communities between logins, so when a user jumps from one amity config to another, it gets to see the communities from both configurations. Can this be fixed, please?

---

<div class="post-metadata">

**Author:** ![Trust](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/trust/32/63_2.png) [@Trust](https://forum.social.plus/u/Trust)\
**Post date:** [October 19, 2021, 1:46am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333/2 "2021-10-19T01:46:15Z")

</div>

Hello Franco,

The data supposed to be wiped when you called “unregisterDevice” function. May you please show me the code snippet when you call registerDevice, unregisterDevice, and setup ?

---

<div class="post-metadata">

**Author:** ![franco](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@franco](https://forum.social.plus/u/franco)\
**Post date:** [October 19, 2021, 2:24am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333/3 "2021-10-19T02:24:41Z")

</div>

After a user logs in, we know their region, so we use that to initialize Amity:

```auto
            AmityCoreClient.setup(
                apiKey = region.getAmityKey(),
                httpEndpoint = AmityRegionalEndpoint.US,
                socketEndpoint = AmityRegionalEndpoint.US
            )

```

And right after, the user:

```auto
AmityCoreClient.registerDevice(user.userId)
                .displayName(displayName = user.username ?: "User")
                .authToken(user.amityToken)
                .build()
                .submit()
            AmityCoreClient.registerDeviceForPushNotification()
                .doOnComplete {
                    Timber.w("Register complete // Amity")
                    sendFirebaseToken()
                }
                .doOnError {
                    Timber.w("Error Register// Amity")
                }
                .subscribe()

```

We then call these two method on each app launch.

And when the users logs out we call:

```auto
            AmityCoreClient.unregisterDevice().subscribe()
            AmityCoreClient.unregisterDeviceForPushNotification().subscribe()

```

---

<div class="post-metadata">

**Author:** ![Trust](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/trust/32/63_2.png) [@Trust](https://forum.social.plus/u/Trust)\
**Post date:** [October 19, 2021, 2:28am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333/4 "2021-10-19T02:28:17Z")

</div>

Thank you Franco,  
We will investigate the issue further and keep you updated.

---

<div class="post-metadata">

**Author:** ![Trust](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/trust/32/63_2.png) [@Trust](https://forum.social.plus/u/Trust)\
**Post date:** [October 25, 2021, 1:37am UTC](https://forum.social.plus/t/cache-issue-on-repositories-when-changing-apikey/333/5 "2021-10-25T01:37:20Z")

</div>

Hello Franco,  
We have investigated the issue and tried replicating the issue and can’t seem to reproduce. May you help us log if the unregisterDevice method is successfully executed by

```auto
AmityCoreClient.unregisterDevice()
               .subscribeOn(Schedulers.io())
               .doOnError { 
                 // log if it's error
                }
               .doOnComplete {
                // log if it's completed
               .subscribe ()

```
