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

Need to register two or more classes to a single Content Type #346

Open
2 of 6 tasks
OrlSan opened this issue Jun 23, 2021 · 0 comments
Open
2 of 6 tasks

Need to register two or more classes to a single Content Type #346

OrlSan opened this issue Jun 23, 2021 · 0 comments

Comments

@OrlSan
Copy link

OrlSan commented Jun 23, 2021

  • contentful.swift version number: 5.5.1
  • Xcode version number: 12.4
  • Target operating system(s) and version number(s)
    • iOS: 13.0 +
    • tvOS:
    • watchOS:
    • macOS:
  • Package manager:
    • Carthage
    • Cocoapods

Hello!

I am currently developing an application with a dependency on Contentful for different subjects such as getting images catalogs, items collections, and in general all of the information content from the API using the contentful.swift SDK.

Our current setup on Contenful is relying on a single Content Type, called "CustomGroup", to do exactly that: Grouping different content Types according to the required information. There is one instance of "CustomGroup" called "Landing Objects" with an array of Entries of a type (Content Type: TypeA), and there is another instance called "Home Objects" with a different array of Entries (Content Type: TypeB).

final class LandingObjects: EntryDecodable, FieldKeysQueryable {
    static var contentTypeId: String = "customGroup"

    var id: String
    var updatedAt: Date?
    var createdAt: Date?
    var localeCode: String?

    var items: [TypeAEntry]?
}
final class HomeObjects: EntryDecodable, FieldKeysQueryable {
    static var contentTypeId: String = "customGroup"

    var id: String
    var updatedAt: Date?
    var createdAt: Date?
    var localeCode: String?

    var items: [TypeBEntry]?
}

If we perform the Client initialize process and register the four classes we get an error as if one of the classes did not get registered.

let contentTypeClasses: [EntryDecodable.Type] = [
    LandingObjects.self, HomeObjects.self, TypeAEntry.self, TypeBEntry.self
]

client = Client(spaceId: "MySpaceID", accessToken: "MyAccessToken", contentTypeClasses: contentTypeClasses)

If we request both objects from the API (we're using the Object ID directly for each instance and is an static value on code) we get an error:

2021-06-23 17:23:41.382070-0500 MyApp[5992:236424] [Contentful] Error: A response for the QueryOn<LandingObjects> did return successfully, but a serious error occurred when decoding the array of LandingObjects.
Double check that you are passing LandingObjects.self, and references to all other EntryDecodable classes into the Client initializer.

So the first Content class we register on the Contentful Client initializer is ignored as if it was never registered. As I could figure out the Contentful Client has a key-value mapping of a Content Type corresponding to a class, so as a temporary workaround we're initializing two separate Contentful Client objects each one of them for an object. Anyway this might not scale well on the longterm since we're planning to use more and more objects this way: Grouping them inside our "container" Content Type and get them whenever they are needed.

Is there anyway there could be more than a single class pointing to a Content Type inside this SDK? Are there any other possible solutions to scale this? Thanks in advance for any information or suggestions!

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

1 participant