GetStream ios 处理响应

GetStream ios handle response

我正在调用一个方法来查看用户是否在关注特定的供稿。我使用的代码是:

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

问题是我无法获得 "result"。我在日志中得到的是:

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

我可以理解get请求是通过Moya并且结果打印在日志中。 那么如何从 feed.following 函数的回调中获取结果并进行处理呢? 谢谢

我刚刚发现 "self" 在 return 回调中为 nil "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)
                                                            }
    }

所以我所做的是在return之前添加let tmp_self = self,并且在回调中,我会使用result.parse(tmp_self.callbackQueue, completion)来避免self为nil。

我其实不知道为什么self在回调中变成了nil,但这解决了我的问题。

如果您保持对 feed 的强引用,则不应释放它。

这种情况下,self会变成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)
  }
}

相反,您需要:

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

希望这对您有所帮助