Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completion handler is invoked twice with two different failure results #333

Open
2 tasks done
jordanhbuiltbyhq opened this issue Mar 18, 2021 · 5 comments
Open
2 tasks done

Comments

@jordanhbuiltbyhq
Copy link

jordanhbuiltbyhq commented Mar 18, 2021

  • contentful.swift version number: 5.4.1
  • Xcode version number: 12.5 (12E5244e)
  • Target operating system(s) and version number(s)
    • iOS: 14.5
  • Package manager:
    • 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
}
@nivbp7
Copy link

nivbp7 commented Jan 27, 2022

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

@anuraagshakya
Copy link

Also experiencing the same issue.

@kaidotarma
Copy link

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

@tristan-warner-smith
Copy link

tristan-warner-smith commented May 12, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants