Callbacks not called on GetStream Swift SDK

Viewed 99

I have a blank Xcode project with GetStream integrated

Podfile :

  pod 'GetStream'

Initialized GetStream on AppDelegate :

Client.config = .init(apiKey: "XXXXX", appId: "XXXX", logsEnabled: true)
Client.shared.setupUser(token: "XXXXX") { (result) in
      // THIS IS SUCCESSFUL, working perfectly
}

On the ViewController, trying to fetch the activities from a feed (which have activities inside)

let userFeed = Client.shared.flatFeed(feedSlug: "FLAT_FEED_ID", userId: "LOGGED_USER_ID")

let activity = Activity(actor: User.current!, verb: "pin", object: "Place:72482734")

userFeed.add(activity) { result in
   print("Callback called") // ----> Never called
   if let activity = try? result.get() {
         print(activity.id) // ----> Never called
   } else {
         print("FAILED") // ----> Never called
   }
}

userFeed.get() { result in
        print(result) // ---->  Never called
}

However, since I have the logsEnabled to true, I'm able to see the raw JSON values in the Xcode Console with all my activities from that feed. The activities are successfully posted to the feed but the closures are just not called.

GetStream Environment

GetStream version: 2.2.2 Xcode version: 11.5 Swift version: 5 Platform(s) running GetStream: iOS 13.5 macOS version running Xcode: macOS 10.15.4

Additional info

I've also tried removing all the pods and installing GetStream using pod 'GetStream', '~> 2.2.2' and pod 'GetStreamActivityFeed'

`

2 Answers

Found the solution, went through the code and found that self becomes nil when the methods are called from the local scope. Creating a strong reference to the feed or activity fixed the issue.

class ViewController {

    var userFeed : FlatFeed! // create strong reference
    var activity : Activity! // create strong reference

    override func viewDidLoad() {
        super.viewDidLoad()
        userFeed = Client.shared.flatFeed(feedSlug: "FLAT_FEED_ID", userId: "LOGGED_USER_ID")

        activity = Activity(actor: User.current!, verb: "pin", object: "Place:72482734")

        userFeed.add(activity) { [weak self] result in
                if let activity = try? result.get() {
                        print(activity.id)
                } else {
                          print("FAILED")
                }
        }

        userFeed.get() {  [weak self] result in
                print(result)
        }
    }

}

The callbacks are stored in the userFeed object which, I assume, is not living long enough to receive the response (going out of scope). To fix this, simply save the userFeed object outside the scope. Example as a ViewController property using your code:

class ViewController: UIViewController {
    let userFeed = Client.shared.flatFeed(feedSlug: "FLAT_FEED_ID", userId: "LOGGED_USER_ID")

    func doTheThing() {
        let activity = Activity(actor: User.current!, verb: "pin", object: .text("Place:72482734"))

        userFeed.add(activity) { result in
            print("Callback called") // ----> Never called
            if let activity = try? result.get() {
                print(activity.id) // ----> Never called
            } else {
                print("FAILED") // ----> Never called
            }
        }

        userFeed.get() { result in
            print(result) // ---->  Never called
        }
    }
}
Related