contentful.swift icon indicating copy to clipboard operation
contentful.swift copied to clipboard

completion handler is invoked twice with two different failure results

Open jordanhbuiltbyhq opened this issue 4 years ago • 5 comments

  • 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
}

jordanhbuiltbyhq avatar Mar 18 '21 19:03 jordanhbuiltbyhq

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

nivbp7 avatar Jan 27 '22 14:01 nivbp7

Also experiencing the same issue.

anuraagshakya avatar Apr 08 '22 15:04 anuraagshakya

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

kaidotarma avatar Apr 13 '22 12:04 kaidotarma

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.

tristan-warner-smith avatar May 12 '22 08:05 tristan-warner-smith

I've created a PR with a patch and a suggestion on tackling the reason behind having it called twice.

tristan-warner-smith avatar May 12 '22 14:05 tristan-warner-smith