求助 Alamofire 的 publisher 用法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
SorryChen
V2EX    Swift/a>

求助 Alamofire 的 publisher 用法

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

    在学习 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     Solana     2237 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 15:55 PVG 23:55 LAX 07:55 JFK 10:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86