contentful.swift
contentful.swift copied to clipboard
completion handler is invoked twice with two different failure results
- contentful.swift version number: 5.4.1
- Xcode version number: 12.5 (12E5244e)
- Target operating system(s) and version number(s)
- [x] iOS: 14.5
- Package manager:
- [x] Swift Package Manager
With the following code:
let query = QueryOn<Resource>
.where(field: .title, .equals("Resource 1"))
contentfulClient.fetchArray(of: Resource.self, matching: query) { result in
print("got result")
print(result)
}
When decoding fails, for example if the content does not contain data at a key you've specified is required, the completion handler is called twice with two different failure results. This is ultimately crashing my app because I wrap this in a DispatchGroup enter() and leave() so it tries to leave twice. This is what's logged:
[Contentful] Error: keyNotFound(FieldKeys(stringValue: "title1", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key FieldKeys(stringValue: \"title1\", intValue: nil) (\"title1\").", underlyingError: nil))
got result
failure(keyNotFound(FieldKeys(stringValue: "title1", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key FieldKeys(stringValue: \"title1\", intValue: nil) (\"title1\").", underlyingError: nil)))
Contentful] Error: Unknown error occured during decoding.
got result
failure(Unknown error occured during decoding.)
keyNotFound(FieldKeys(stringValue: "title1", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key FieldKeys(stringValue: \"title1\", intValue: nil) (\"title1\").", underlyingError: nil))
Here's my decoder:
public required init(from decoder: Decoder) throws {
let fields = try decoder.contentfulFieldsContainer(keyedBy: FieldKeys.self)
title1 = try fields.decode(String.self, forKey: .title1) //there is no title1 so this fails
}
I am experiencing the same issue.
- contentful.swift version number: 5.5.1
- Xcode version number: 12.5.1 (12E507)
- Target operating system(s) and version number(s) iOS: 14.5
- Package manager: Swift Package Manager
I've created a gist with a code sample
Also experiencing the same issue.
Seems that this crash popped up again on Apr 13, might be related to "Issues with the Contentful Web App and Asset rendering" https://www.contentfulstatus.com/incidents/0rqc2b850qg3
I just encountered this because I've wrapped fetchArray(of in a Task and this triggers Fatal error: SWIFT TASK CONTINUATION MISUSE: fetchArray(of:) tried to resume its continuation more than once, throwing Unknown error occured during decoding.!
The issue is in Client.swift handleJSON<DecodableType: Decodable>.
If a failure happens on the original decode the failure completion is called but not returned so it continues to a second decoded check that fails and again calls completion(.failure(error).
Client.swift:476 and 500 in Contentful 5.5.2.
I've created a PR with a patch and a suggestion on tackling the reason behind having it called twice.