【问题标题】:GetStream ios handle responseGetStream ios 处理响应
【发布时间】:2020-01-13 16:23:11
【问题描述】:

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

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

问题是我无法得到“结果”。我在日志中得到的是:

Moya_Logger:[13/01/2020 11:08:34] 请求: 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] 请求标头:[“授权”: “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 请求方法:GET Moya_Logger:[13/01/2020 11:08:34] 响应:{ URL: https://api.stream-io-api.com/api/v1.0/feed/public/55Crx1RIzGasE1p3E1RK8DpWlMm1/follows/?api_key=n9asnsfv92be&filter=public%3A7YZSZNpYOMU2GyRcxS1x152loPW2&limit=2&offset=0 } { 状态码:200,标题 { “访问控制允许来源”=( “*” ); “缓存控制” = ( “无缓存” ); “内容编码” = ( 压缩包 ); “内容长度” = ( 182 ); “内容类型” = ( “应用程序/json;字符集=utf-8” ); 日期 = ( “格林威治标准时间 2020 年 1 月 13 日星期一 16:08:34” ); 服务器 = ( nginx ); “访问控制允许标头”=( “x-requested-with、content-type、accept、origin、authorization、x-csrftoken、x-stream-client、stream-auth-type” ); “访问控制允许方法”=( “获取、发布、放置、修补、删除、选项” ); “访问控制最大年龄”=( 86400 ); "x-ratelimit-limit" = ( 500 ); "x-ratelimit-remaining" = ( 499 ); "x-ratelimit-reset" = ( 1578931740 ); } } {"结果":[{"feed_id":"public:55Crx1RIzGasE1p3E1RK8DpWlMm1","target_id":"public:7YZSZNpYOMU2GyRcxS1x152loPW2","created_at":"2019-12-20T20:24:59.359562691Z","updated_at":" -12-20T20:24:59.359562691Z"}],"持续时间":"0.89ms" }

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

【问题讨论】:

    标签: swift getstream-io moya


    【解决方案1】:

    如果您强烈引用 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)
      }
    }
    

    希望对你有帮助

    【讨论】:

    • 是的,这是避免提要被释放的好方法。谢谢。
    【解决方案2】:

    我刚刚在“Feed+Following.swift”中发现“self”在返回回调中为零。

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2017-08-13
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多