Skip to content

Commit 79a1151

Browse files
committed
Hotfix EXC_BAD_ACCESS with struct workaround.
1 parent a51721c commit 79a1151

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Sources/Request/Request/Request.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Combine
3030
/// - Precondition: The `Request` body must contain **exactly one** `Url`
3131
public typealias Request = AnyRequest<Data>
3232

33+
// TODO: Fix EXC_BAD_ACCESS instead of workaround with `struct`
3334
/// Tha base class of `Request` to be used with a `Codable` `ResponseType` when using the `onObject` callback
3435
///
3536
/// *Example*:
@@ -40,8 +41,7 @@ public typealias Request = AnyRequest<Data>
4041
/// .onObject { myCodableStructs in
4142
/// ...
4243
/// }
43-
public class AnyRequest<ResponseType>: ObservableObject, Identifiable where ResponseType: Decodable {
44-
public var willChange = PassthroughSubject<AnyRequest, Never>()
44+
public struct AnyRequest<ResponseType>/*: ObservableObject, Identifiable*/ where ResponseType: Decodable {
4545

4646
private var params: CombinedParams
4747

@@ -51,7 +51,7 @@ public class AnyRequest<ResponseType>: ObservableObject, Identifiable where Resp
5151
private var onObject: ((ResponseType) -> Void)?
5252
private var onError: ((RequestError) -> Void)?
5353

54-
@Published public var response: Response = Response()
54+
/*@Published*/ public var response: Response = Response()
5555

5656
public init(@RequestBuilder builder: () -> RequestParam) {
5757
let params = builder()
@@ -68,34 +68,43 @@ public class AnyRequest<ResponseType>: ObservableObject, Identifiable where Resp
6868
self.response = Response()
6969
}
7070

71+
internal init(params: CombinedParams,
72+
onData: ((Data) -> Void)?,
73+
onString: ((String) -> Void)?,
74+
onJson: ((Json) -> Void)?,
75+
onObject: ((ResponseType) -> Void)?,
76+
onError: ((RequestError) -> Void)?) {
77+
self.params = params
78+
self.onData = onData
79+
self.onString = onString
80+
self.onJson = onJson
81+
self.onObject = onObject
82+
self.onError = onError
83+
}
84+
7185
/// Sets the `onData` callback to be run whenever `Data` is retrieved
7286
public func onData(_ callback: @escaping (Data) -> Void) -> Self {
73-
self.onData = callback
74-
return self
87+
Self.init(params: params, onData: callback, onString: onString, onJson: onJson, onObject: onObject, onError: onError)
7588
}
7689

7790
/// Sets the `onString` callback to be run whenever a `String` is retrieved
7891
public func onString(_ callback: @escaping (String) -> Void) -> Self {
79-
self.onString = callback
80-
return self
92+
Self.init(params: params, onData: onData, onString: callback, onJson: onJson, onObject: onObject, onError: onError)
8193
}
8294

8395
/// Sets the `onData` callback to be run whenever `Json` is retrieved
8496
public func onJson(_ callback: @escaping (Json) -> Void) -> Self {
85-
self.onJson = callback
86-
return self
97+
Self.init(params: params, onData: onData, onString: onString, onJson: callback, onObject: onObject, onError: onError)
8798
}
8899

89100
/// Sets the `onObject` callback to be run whenever `Data` is retrieved
90101
public func onObject(_ callback: @escaping (ResponseType) -> Void) -> Self {
91-
self.onObject = callback
92-
return self
102+
Self.init(params: params, onData: onData, onString: onString, onJson: onJson, onObject: callback, onError: onError)
93103
}
94104

95105
/// Handle any `RequestError`s thrown by the `Request`
96106
public func onError(_ callback: @escaping (RequestError) -> Void) -> Self {
97-
self.onError = callback
98-
return self
107+
Self.init(params: params, onData: onData, onString: onString, onJson: onJson, onObject: onObject, onError: callback)
99108
}
100109

101110
/// Performs the `Request`, and calls the `onData`, `onString`, `onJson`, and `onError` callbacks when appropriate.

Sources/Request/SwiftUI/RequestView/RequestView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public struct RequestView<Content, Placeholder> : View where Content: View, Plac
3737
}
3838

3939
public var body: some View {
40-
if data == nil || oldReq == nil || oldReq?.id != request.id {
40+
if data == nil/* || oldReq == nil || oldReq?.id != request.id*/ {
4141
let req = self.request.onData { data in
4242
self.oldReq = self.request
4343
self.data = data

0 commit comments

Comments
 (0)