V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
SorryChen
V2EX  ›  Swift

求助 Alamofire 的 publisher 用法

  •  
  •   SorryChen · 2021-12-03 08:44:09 +08:00 · 1330 次点击
    这是一个创建于 846 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在学习 macOS 开发想捣鼓个小东西自己用,零基础。这两天研究了研究 Publisher ,感觉也算是大概懂了,但是在用 Alamofire 的 publisher 的时候,怎么也搞不定了。比如如下这个只有一个函数的 APP ,为什么请求不成功呢:

    
    import SwiftUI
    
    import Alamofire
    
    struct ContentView: View {
        var body: some View {
            Text("Hello, world!")
                .padding()
                .onAppear(perform: load)
        }
        
        struct TestResponse: Decodable {
            let userId: Int
            let id: Int
            let title: String
            let body: String
        }
        
        func load(){
            AF.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil)
                .validate()
                .publishDecodable(type: [TestResponse].self)
                .print()
                .sink { print($0) }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    

    打印出来的是:

    receive subscription: (Alamofire.DataResponsePublisher<Swift.Array<Test.ContentView.TestResponse>>.(unknown context at $1080f8314).Inner<Combine.Publishers.Print<Alamofire.DataResponsePublisher<Swift.Array<Test.ContentView.TestResponse>>>.(unknown context at $7ff81332d748).Inner<Combine.Subscribers.Sink<Alamofire.DataResponse<Swift.Array<Test.ContentView.TestResponse>, Alamofire.AFError>, Swift.Never>>>)
    request unlimited
    receive cancel
    receive value: (failure(Alamofire.AFError.explicitlyCancelled))
    receive finished
    

    为什么会出现 explicitlyCancelled 呢,谢谢大家。

    第 1 条附言  ·  2021-12-03 09:35:54 +08:00
    似乎找到了解释

    As with all Combine usage, care must be taken to ensure that subscriptions are not cancelled early by maintaining the lifetime of the AnyCancellable tokens returned by functions like sink. If a request is cancelled prematurely, the response's error will be set to AFError.explicitlyCancelled.
    第 2 条附言  ·  2021-12-03 09:47:12 +08:00
    找到了答案,
    只需使用一个如 State 来维护 sink 返回的 Cancellable 的生命周期就好了,结贴。
    paopaosa
        1
    paopaosa  
       2021-12-03 08:51:45 +08:00
    打开外网访问的权限了吗?
    SorryChen
        2
    SorryChen  
    OP
       2021-12-03 08:54:05 +08:00
    @paopaosa 额谢谢,好像没注意这个事情,我去研究研究,纯 mac 开发新手哈哈
    SorryChen
        3
    SorryChen  
    OP
       2021-12-03 08:58:25 +08:00
    @paopaosa 您好,在弄了权限之后发现,使用如下方式可以收到数据:

    ```
    AF.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil)
    .response { r in
    print(r.data)
    }
    ```

    但是 publisher 还是不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2558 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.