GetStream / stream-swift

Swift client for Stream API
https://getstream.io
BSD 3-Clause "New" or "Revised" License
35 stars 26 forks source link

Closures / Callbacks of GetStream methods are not called #26

Closed Parithi closed 4 years ago

Parithi commented 4 years ago

What did you do?

Integrated GetStream to Xcode Project

  pod 'Alamofire'
  pod 'GetStream', '~> 2.2.2'

Initialized GetStream on AppDelegate :

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

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
    if let activity = try? result.get() {
          print(activity.id)
     } else {
          print("FAILED")
     }
}

userFeed.get() { result in
        print(result)
}

What did you expect to happen?

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

userFeed.get() { result in
        print(result) // ----------> This should print out a result?
}

What happened instead?

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 context

I've also tried removing all the pods and installing GetStream using pod 'GetStream' instead of version 2.2.2, but I have the same issue even on a fresh install.

Parithi commented 4 years ago

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)
        }
    }

}
melhor83 commented 4 years ago

Thank you @Parithi had the same problem and you saved me tons of time in debugging!