简体   繁体   中英

AWS AppSync multiple subscriptions in same view controller not working - iOS Swift

Just FYI I posted this question originally in the AWS AppSync forum (in case in the future AWS answers it).

I have been trying to make a simple Posts app like the one in the docs but I have found no documentation or guides that handle multiple subscriptions in one view controller.

Three mutations: onCreatePost, onUpdatePost, onDeletePost (and of course three subscriptions to those mutations)

In Xcode, I have three functions called during viewDidLoad(): subscribeToNewPosts(), subscribeToUpdatedPosts(), subscribeToDeletedPosts()

Each subscription function works and creates a subscription with the correct functionality and updates the table view accordingly if used alone . But, if called one after the other, only the last subscription will actually receive data and update the table view. I put a breakpoint to check out topicSubscribersDictionary in AppSyncMQTTClient.swift after subscribing to all three mutations

func startNewSubscription(subscriptionInfo: AWSSubscriptionInfo) {
        var topicQueue = [String]()
        let mqttClient = MQTTClient<AnyObject, AnyObject>()
        mqttClient.clientDelegate = self
        for topic in subscriptionInfo.topics {
            if topicSubscribersDictionary[topic] != nil {
                // if the client wants subscriptions and is allowed we add it to list of subscribe
                topicQueue.append(topic)
            }
        }
        mqttClients.append(mqttClient)
        mqttClientsWithTopics[mqttClient] = topicQueue
        mqttClient.connect(withClientId: subscriptionInfo.clientId, toHost: subscriptionInfo.url, statusCallback: nil)
    }

and all three subscriptions are in fact in the dictionary...

Do I need multiple instances of appSyncClient, one for each subscription? Is it a problem with the schema design?

schema.graphql

schema.json

mutations.graphql

queries.graphql

subscriptions.graphql

Example use case: simple chat app. New conversation started = OnCreatePostSubscription; new incoming message in that conversation = OnUpdatePostSubscription

Are you using API Key for authorization in AppSync ? If you are using API Key only one subscription is supported by the SDK at this point. Could you switch to IAM (Cognito Identity) or Cognito UserPools based auth and see if multiple subscriptions work for you?

I managed to have several subscriptions working with API Key by replacing the call startSubscriptions to startNewSubscription inside AWSAppSyncSubscriptionWatcher

if let subscriptionInfo = subscriptionResult.subscrptionInfo {
    self.subscriptionTopic = subscriptionResult.newTopics
    self.client?.addWatcher(watcher: self, topics: subscriptionResult.newTopics!, identifier: self.uniqueIdentifier)
    //self.client?.startSubscriptions(subscriptionInfo: subscriptionInfo)
    subscriptionInfo.forEach { self.client?.startNewSubscription(subscriptionInfo: $0) }
}

Couldn't find any side effect with this approach yet, apart from requiring to fork the iOS SKD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM