yueheng shi
yueheng shi

Reputation: 31

GetStream ios handle response

I'm calling a method to see if a user is following a specific feed. The code I'm using is:

let feed = Client.shared.flatFeed(feedSlug: "public", userId: user.uid)
feed.following(filter: [FeedId(feedSlug: "public", userId: "7YZSZNpYOMU2GyRcxS1x152loPW2")], limit: 1) { result in
        print(result)
    }

The problem is that I couldn't get the "result". And what I get in the log is:

Moya_Logger: [13/01/2020 11:08:34] Request: https://api.stream-io-api.com/api/v1.0/feed/public/55Crx1RIzGasE1p3E1RK8DpWlMm1/follows/?api_key=n9asnsfv92be&filter=public%3A7YZSZNpYOMU2GyRcxS1x152loPW2&limit=2&offset=0

Moya_Logger: [13/01/2020 11:08:34] Request Headers: ["Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNTVDcngxUkl6R2FzRTFwM0UxUks4RHBXbE1tMSJ9.C8gYvepNOr14qgnXRATA2exBCvNXgmD3pI51OyF_n7U", "X-Stream-Client": "stream-swift-client-2.0.0", "Stream-Auth-Type": "jwt"] Moya_Logger: [13/01/2020 11:08:34] HTTP Request Method: GET Moya_Logger: [13/01/2020 11:08:34] Response: { URL: https://api.stream-io-api.com/api/v1.0/feed/public/55Crx1RIzGasE1p3E1RK8DpWlMm1/follows/?api_key=n9asnsfv92be&filter=public%3A7YZSZNpYOMU2GyRcxS1x152loPW2&limit=2&offset=0 } { Status Code: 200, Headers { "Access-Control-Allow-Origin" = ( "*" ); "Cache-Control" = ( "no-cache" ); "Content-Encoding" = ( gzip ); "Content-Length" = ( 182 ); "Content-Type" = ( "application/json;charset=utf-8" ); Date = ( "Mon, 13 Jan 2020 16:08:34 GMT" ); Server = ( nginx ); "access-control-allow-headers" = ( "x-requested-with, content-type, accept, origin, authorization, x-csrftoken, x-stream-client, stream-auth-type" ); "access-control-allow-methods" = ( "GET, POST, PUT, PATCH, DELETE, OPTIONS" ); "access-control-max-age" = ( 86400 ); "x-ratelimit-limit" = ( 500 ); "x-ratelimit-remaining" = ( 499 ); "x-ratelimit-reset" = ( 1578931740 ); } } {"results":[{"feed_id":"public:55Crx1RIzGasE1p3E1RK8DpWlMm1","target_id":"public:7YZSZNpYOMU2GyRcxS1x152loPW2","created_at":"2019-12-20T20:24:59.359562691Z","updated_at":"2019-12-20T20:24:59.359562691Z"}],"duration":"0.89ms" }

I can understand the get request is through Moya and the result is printed in the log. So how can I get the result and handle it from the callback in feed.following function? Thanks

Upvotes: 0

Views: 288

Answers (2)

bonc
bonc

Reputation: 152

If you're keeping a strong reference to the feed it shouldn't get deallocated.

This case, self will become nil:

override func viewDidLoad() {
  super.viewDidLoad()
  let feed = Client.shared.flatFeed(feedSlug: "public", userId: user.uid)
  feed.following(filter: [FeedId(feedSlug: "public", userId: "7YZSZNpYOMU2GyRcxS1x152loPW2")], limit: 1) { result in
    print(result)
  }
}

Instead, you need to:

let feed = Client.shared.flatFeed(feedSlug: "public", userId: user.uid)
override func viewDidLoad() {
  super.viewDidLoad()
  feed.following(filter: [FeedId(feedSlug: "public", userId: "7YZSZNpYOMU2GyRcxS1x152loPW2")], limit: 1) { result in
    print(result)
  }
}

Hope this helps

Upvotes: 2

yueheng shi
yueheng shi

Reputation: 31

I just found "self" is nil in return callback in "Feed+Following.swift".

return client.request(endpoint: FeedEndpoint.following(feedId,
                                                       filter: filter,
                                                       offset: offset,
                                                       limit: limit)) { [weak self] result in
                                                            if let self = self {
                                                                result.parse(self.callbackQueue, completion)
                                                            }
    }

So what I did is to add let tmp_self = self before return, and in the callback, I would use result.parse(tmp_self.callbackQueue, completion) to avoid self is nil.

I actually have no idea why self becomes nil in the callback, but this solved my issue.

Upvotes: 0

Related Questions