Swift 如何使用combine返回fetch调用的结果?
我是一个新的结合,并努力了解如何才能返回我的获取PIN的结果 我可以将结果设置为Swift 如何使用combine返回fetch调用的结果?,swift,combine,Swift,Combine,我是一个新的结合,并努力了解如何才能返回我的获取PIN的结果 我可以将结果设置为@Published,但我只希望能够调用fetch方法并等待结果或错误 class PinService: NSObject, ObservableObject { private var session: Session private var subscriptions = Set<AnyCancellable>() init(session: Session) {
@Published
,但我只希望能够调用fetch方法并等待结果或错误
class PinService: NSObject, ObservableObject {
private var session: Session
private var subscriptions = Set<AnyCancellable>()
init(session: Session) {
self.session = session
super.init()
}
func fetchPins (categories: Set<CategoryModel>, coordinates: CLLocationCoordinate2D) {
_fetchPins(categories: categories, coordinates: coordinates)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("fetchPins() error")
case .finished:
print("fetchPins() complete")
}
}, receiveValue: { pins in
/*
What to do here?
I can add a @Published var pins: [Pin], and do
self.pins = pins
But if I want to be able to return the value or the error, how can I do that?
*/
})
.store(in: &self.subscriptions)
}
private func _fetchPins(categories: Set<CategoryModel>, coordinates: CLLocationCoordinate2D) -> Future<[Pin], Error> {
return Future<[Pin], Error> { promise in
let categoryIds = categories.map { return $0.id }.joined(separator: ",")
let radius = 15 //miles
self.session.request(baseUrl + "/api/v1/pinsRadius?latitude=\(coordinates.latitude)&longitude=\(coordinates.longitude)&radius=\(radius)&categories=\(categoryIds)")
.responseDecodable(of: [Pin].self) { (response: DataResponse) in
switch response.result {
case .success(let pins):
promise(.success((pins)))
case .failure(let error):
promise(.failure(error))
}
}
}
}
}
class PinService:NSObject,ObservableObject{
专用var会话:会话
私有变量订阅=Set()
初始化(会话:会话){
self.session=会话
super.init()
}
func fetchPins(类别:集合,坐标:CLLocationCoordinate2D){
_FetchPin(类别:类别,坐标:坐标)
.sink(receiveCompletion:{completion in
交换完成{
案例.失败:
打印(“fetchPins()错误”)
案例。完成:
打印(“fetchPins()完成”)
}
},receiveValue:{pins in
/*
在这里做什么?
我可以添加@Published var pins:[Pin],然后
self.pins=pins
但是如果我想返回值或错误,我该怎么做呢?
*/
})
.store(在:&self.subscriptions中)
}
私有函数(类别:集合,坐标:CLLocationCoordinate2D)->未来{
回报未来{承诺未来}
让categoryIds=categories.map{return$0.id}.joined(分隔符:“,”)
让半径=15//英里
self.session.request(baseUrl+“/api/v1/pinsRadius?纬度=\(坐标.纬度)&经度=\(坐标.经度)&半径=\(半径)&类别=\(类别ID)”)
.responseDecodable(属于:[Pin].self){(响应:DataResponse)位于
开关响应。结果{
案例。成功(让别针):
承诺(.success((pins)))
案例。失败(let错误):
承诺(失败(错误))
}
}
}
}
}
抱歉,如果这是一个愚蠢的问题,谢谢。一个解决方案是在函数中使用完成处理程序,并从
receiveValue
func fetchPins (categories: Set<CategoryModel>,
coordinates: CLLocationCoordinate2D,
completion: @escaping (Result<[Pin], Error>) -> Void)) {
_fetchPins(categories: categories, coordinates: coordinates)
.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
completion(.failure(error))
print("fetchPins() error")
case .finished:
print("fetchPins() complete")
}
}, receiveValue: { pins in
completion(.success(pins))
})
.store(in: &self.subscriptions)
}
谢谢我怎样才能发送错误呢?我在receiveValue块中没有错误。您可以将完成处理程序的参数类型从[Instrument]更改为Result,然后使用.success或.failure调用它
.store(in:&self.subscriptions)
的目的是什么如果PIN已存储在完成中:完成(.success(pins)
?我明白了,订阅是storedOk。因此,只有在您还想存储PIN之外的订阅(在本例中)时才需要它?。
fetchPins(categories: someValue, coordinates: someOtherValue) { pins in
//do stuff with pins here
}