# Error occured when fetch using SyncManager ChannelCollection (

**URL:** <https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064>\
**Category:** Android\
**Tags:** open-channel, group-channel, syncmanager, chatsdk\
**Created:** [September 29, 2020, 7:36am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064 "2020-09-29T07:36:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zulfikar18](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zulfikar18](https://community.sendbird.com/u/zulfikar18)\
**Post date:** [September 29, 2020, 7:36am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/1 "2020-09-29T07:36:40Z")

</div>

I am use Sendbird syncmanager v1.1.22 and i get ConcurrentModificationException when calling fetch function on ChannelCollection, i don’t really know how could it happen, this is the log list that show when its happen:

```
Caused by java.util.ConcurrentModificationException
       at java.util.ArrayList$Itr.next(ArrayList.java:860)
       at java.util.AbstractCollection.toString(AbstractCollection.java:461)
       at java.lang.String.valueOf(String.java:2924)
       at java.lang.StringBuilder.append(StringBuilder.java:132)
       at com.sendbird.syncmanager.DatabaseController.getGroupChannelListQueryFromDb(DatabaseController.java:1202)
       at com.sendbird.syncmanager.ChannelContainer.getGroupChannelListQuery(ChannelContainer.java:200)
       at com.sendbird.syncmanager.ChannelCollection.fetch(ChannelCollection.java:208)
       at com.sendbird.syncmanager.ChannelCollection.fetch(ChannelCollection.java:117)

```

any suggestion how to resolve this problem?  
Thanks

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [September 29, 2020, 8:05am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/2 "2020-09-29T08:05:25Z")

</div>

Hi @zulfikar18, thank you for reporting this problem to us.

It seems that the problem originated from `GroupChannelListQuery#mCustomTypes` being updated while the sync manager is reading data from it. We will fix this as soon as possible.

Before fixing it on our end, if you are writing to `GroupChannelListQuery#mCustomTypes`, trying not to write to `GroupChannelListQuery#mCustomTypes` might help. This value can be acquired by `GroupChannelListQuery#getCustomTypesFilter` and `GroupChannelChangeLogsParams#getCustomTypes`. If you are writing to a list acquired by one of these methods, try not to write to them while you are waiting for the fetch. It would be great if this solves your problem, but the problem could have originated from our end so there is no guarantee.

Thank you again for the bug report and please feel free to post follow-up questions or additional inquiries.

---

<div class="post-metadata">

**Author:** ![wisnup](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/wisnup/32/689_2.png) [@wisnup](https://community.sendbird.com/u/wisnup)\
**Post date:** [September 29, 2020, 8:30am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/3 "2020-09-29T08:30:42Z")

</div>

The exception sometimes happened even if we don’t write the custom type again. Basically, this is the code:

```
    val query = GroupChannel.createMyGroupChannelListQuery()
    query.isIncludeEmpty = isIncludeEmptyGroup
    query.limit = queryLimit
    query.customTypesFilter = customTypes
    val channelCollection = ChannelCollection(query)
    ...
    channelCollection.fetch(handler)
```

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [September 29, 2020, 8:47am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/4 "2020-09-29T08:47:59Z")

</div>

Hi @wisnup , thank you for the additional info. It helps us track down the causes. It seems that the bug comes from within the sync manager. We will fix it and update it here once the bugfix is released. While waiting for the release, if you need a workaround, please consider catching the exception and trying `fetch` again. I’m sorry that this is not the most elegant solution. Thank you.

---

<div class="post-metadata">

**Author:** ![wisnup](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/wisnup/32/689_2.png) [@wisnup](https://community.sendbird.com/u/wisnup)\
**Post date:** [September 29, 2020, 8:52am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/5 "2020-09-29T08:52:53Z")

</div>

Catching exception is not possible because the SendBird fetch process run on different thread.

```
    private void fetch(final int limit, final CompletionHandler handler) {
        Logger.d(this.getName() + " fetch() limit = " + limit);
        this.runOnSingleThreadPool(new Runnable() {
             public void run() {

```

Unless you mean UncaughtException. We consider that too but yeah we’re still looking for better solution.

---

<div class="post-metadata">

**Author:** ![zulfikar18](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zulfikar18](https://community.sendbird.com/u/zulfikar18)\
**Post date:** [September 29, 2020, 9:08am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/6 "2020-09-29T09:08:08Z")

</div>

@Cjeon according to this issue, do you have any estimated time to release new version for fixing this issue, so we can decide what solution that we take, if it take longer time we will use your suggestion to catch the exception by use this method, [https://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread](https://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread)

```auto
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("Caught " + e);
    }
});

```

and trying to recalling fetch function, what do you thing with this solution?

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [September 29, 2020, 9:39am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/7 "2020-09-29T09:39:25Z")

</div>

Thank you for the reply, @wisnup and @zulfikar18.

It’s hard for me to pinpoint the time when the fix will be released, however, I don’t think we can release a fix for this within this week. (Many of us, including me, are on leave from tomorrow until next week. It’s holiday here.)

What I first intended was that, if an exception is passed to the handler (which you passed to `fetch`), then you could retry like below.

```java
final AtomicBoolean fetchSucceeded = new AtomicBoolean(false);
final int MAX_RETRY_COUNT = 3;
// try until fetch succeeds.
for (int trial = 0; trial < MAX_RETRY_COUNT; trial++) {
    if (fetchSucceeded.get()) break;
    final CountDownLatch fetchFinished = new CountDownLatch(1);
    channelCollection.fetch(new CompletionHandler() {
        @Override
        public void onCompleted(final SendBirdException e) {
            if (e != null) {
                fetchFinished.countDown();
                return;
            }
            fetchSucceeded.set(true);
            // TODO handle success
            fetchFinished.countDown();
        }
    });
    fetchFinished.await();
}

```

(I used the count down latch for explanation purposes but you don’t need to block current thread. the purpose is to keep calling the `fetch` until it succeeds, after the handler is called.)

If the exception is not passed to the handler, then I think you could make use of `Thread.setDefaultUncaughtExceptionHandler`, until we release a fix.

I’m not sure when these problem occur nor the internal status of the sync manager when the problem happens yet, so I’m not quite confident if above methods would work (for example fetch might keep failing if this is caused by some troublesome internal status error).

I’ll try to reproduce the error, and if you find a way that can reproduce this error, please let me know. After succeeding in the bug reproduction, I’ll post the workaround here. Thank you again for participating in the discussion.

---

<div class="post-metadata">

**Author:** ![wisnup](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/wisnup/32/689_2.png) [@wisnup](https://community.sendbird.com/u/wisnup)\
**Post date:** [September 29, 2020, 9:51am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/8 "2020-09-29T09:51:09Z")

</div>

The exception is not passed to the CompletionHandler. It cause our app to be force closed. That’s why we think of `Thread.setDefaultUncaughtExceptionHandler`. As for how to reproduce, we also don’t know, because sometimes it happens, sometimes it works.

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [September 29, 2020, 10:05am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/9 "2020-09-29T10:05:21Z")

</div>

@wisnup thank you for the reply. I think it will require some digging for me to find out what the root cause is. Please consider using `Thread.setDefaultUncaughtExceptionHandler` until we release a fix. Will update here if I make any progress or there’s something to share (ex: error was reproduced, possible workaround found, release is scheduled… etc). I’m sorry that this is the best I can share for now.

---

<div class="post-metadata">

**Author:** ![zulfikar18](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zulfikar18](https://community.sendbird.com/u/zulfikar18)\
**Post date:** [September 29, 2020, 10:06am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/10 "2020-09-29T10:06:33Z")

</div>

@Cjeon Thank you for your really fast response, as @wisnup said that it can be make our app become FC, so for now we will catching the exception until new version that fix this bug release, we will also update in here if we found any information about this issue

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [September 29, 2020, 10:12am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/11 "2020-09-29T10:12:37Z")

</div>

@zulfikar18 sorry that I was not able to provide a quick fix. Thank you for your patience & understanding 😁

---

<div class="post-metadata">

**Author:** ![Cjeon](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/cjeon/32/222_2.png) [@Cjeon](https://community.sendbird.com/u/Cjeon)\
**Post date:** [October 6, 2020, 6:46am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/12 "2020-10-06T06:46:01Z")

</div>

Hi @zulfikar18 and @wisnup. Thank you for waiting. The version that contains a possible fix (hopefully) for this is just released. Let me know if the new version fixes the bug! Please find the new version [here](https://github.com/sendbird/SendBird-SDK-Android/releases/tag/v3.0.148).

---

<div class="post-metadata">

**Author:** ![wisnup](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/wisnup/32/689_2.png) [@wisnup](https://community.sendbird.com/u/wisnup)\
**Post date:** [October 7, 2020, 7:15am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/13 "2020-10-07T07:15:13Z")

</div>

Thank you for the fix (hopefully 😊). We will update it and let you know whether the fix working or not.

---

<div class="post-metadata">

**Author:** ![zulfikar18](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zulfikar18](https://community.sendbird.com/u/zulfikar18)\
**Post date:** [October 7, 2020, 7:25am UTC](https://community.sendbird.com/t/error-occured-when-fetch-using-syncmanager-channelcollection/1064/14 "2020-10-07T07:25:10Z")

</div>

Thank you for the hard work @Cjeon, we will update it here for the result
