SyncManager does not cache messages

I am using SyncManager to cache the channels and messages so when the Internet is not available, users can still go through the cached channels/messages.

The channels seem to cache and retrieved fine, but I never got messages cached. Every time I load the messages, it would actually make call to SendBird for all the messages and then just return those. If I turn off the network (wifi and mobile data), then it would not fetch the messages at all. I checked the SQLite file and it seem to have saved the messages correctly, I think it somehow cannot fetch the messages from the database?

I am using SyncManager(1.1.36) and SendBirdSDK(3.0.203). My code looks like this:

func viewDidLoad() {
    super.viewDidLoad()
    SBSMSyncManager.resumeSynchronize()
    refreshMessages()
}

func refreshMessages() {
    self.collection?.delegate = nil
    self.collection = nil

    let filter = SBSMMessageFilter(messageType: .all, customType: nil, senderUserIds: nil)
    let collection = SBSMMessageCollection(channel: channel, filter: filter, viewpointTimestamp: Int64.max)
    collection.delegate = self
    self.collection = collection

    collection.fetchPendingMessages { [weak self] (error) in
        if let error = error {
            print("Failed to fetch pending messages. Error: \(error)")
        }
    }

    // Fetch all the messages.
    collection.fetch(in: .next, completionHandler: { [weak self] (_, error) in
        if let error = error {
            print("Error fetching chat messages. Error: \(error)")
        }
    })

    // Fetch all the failed messages as well.
    collection.fetchFailedMessages({ (error) in
        if let error = error {
            self.log("Error fetching failed messages. Error: \(error)", level: .debug)
        }
    })
}

func collection(_ collection: SBSMMessageCollection, didReceive action: SBSMMessageEventAction, pendingMessages: [SBDBaseMessage]) {
    let newMessages = pendingMessages.compactMap({ "\nMessage: \($0.message): \($0.createdAt)" })
    print("MsgClction didReceive action: \(action.rawValue), pendingMessages: \(newMessages)")
    switch action {
    case .insert:
        datasource.insert(by: pendingMessages)
    case .update:
        datasource.update(messages: pendingMessages)
    case .remove:
        datasource.remove(messages: pendingMessages)
    default:
        break
    }
}

func collection(_ collection: SBSMMessageCollection, didReceive action: SBSMMessageEventAction, succeededMessages: [SBDBaseMessage]) {
    let newMessages = succeededMessages.compactMap({ "\nMessage: \($0.message): \($0.createdAt)" })
    print("MsgClction didReceive action: \(action.rawValue), succeededMessages: \(newMessages)")

    switch action {
    case .insert:
        self.datasource.insert(by: succeededMessages)
    case .update:
        datasource.update(messages: succeededMessages)
    case .remove:
        datasource.remove(messages: succeededMessages)
    default:
        break
    }
}

func collection(_ collection: SBSMMessageCollection, didReceive action: SBSMMessageEventAction, failedMessages: [SBDBaseMessage],
                reason: SBSMFailedMessageEventActionReason) {
    let newMessages = failedMessages.compactMap({ "\nMessage: \($0.message): \($0.createdAt)" })
    print("MsgClction didReceive action: \(action.rawValue), failedMessages: \(newMessages). Reason: \(reason)")

    switch action {
    case .insert:
        datasource.insert(by: failedMessages)
    case .update:
        datasource.update(messages: failedMessages)
    case .remove:
        datasource.remove(messages: failedMessages)
    default:
        break
    }
}

@chauhan130,

First off, welcome to the Sendbird Community. We’re happy to have you here. Secondly, I’d like to apologize for the delay. I’m not as familiar with Swift as I am with other languages so I’ll do my best to get you accurate answers. I’m going to see if I can reproduce this and I’ll circle back.

Thanks,
Tyler

1 Like

Do we have any updates about this?