2

I'm trying to display some FAQs content in my app using contentful, below is my UserFAQs.swift class which I'm using to fetch the data and this works fine. Now I have a new requirement for adding FAQs for guest user. Only the data is different per user all other things are the same. So, I created a duplicate content model of my existing userFAQs called guestFAQs and added the content for it. In my below class here they both work individually for eg: if I set contentTypeId = “guestFAQs” or contentTypeId = "userFAQs" in my below class it works fine. But I need to fetch based on the user type and when I set the contentTypeId based on the condition below it always gives me the result as "userFAQs" even when the userType is guest. Am I missing something here? Any help is appreciated. This is the library that I'm using.

import UIKit
import Contentful

final class UserFAQs: EntryDecodable, FieldKeysQueryable, Resource {

    static var contentTypeId = (Utilities.userType == .normalUser) ? "userFAQs" : "guestFAQs"

    let sys: Sys
    var sectionIndex: Int?
    var sectionName: String?
    var questionName: String?
    var answer: String?

    // MARK: Initialization
    public required init(from decoder: Decoder) throws {
        print("APIName = \(UserFAQs.contentTypeId)")
        sys         = try decoder.sys()
        let fields      = try decoder.contentfulFieldsContainer(keyedBy: UserFAQs.FieldKeys.self)

        sectionIndex       = try fields.decodeIfPresent(Int.self, forKey: .sectionIndex)
        sectionName      = try fields.decodeIfPresent(String.self, forKey: .sectionTitle)
        questionName       = try fields.decodeIfPresent(String.self, forKey: .questionName)
        answer    = try fields.decodeIfPresent(String.self, forKey: .sectionText)
    }

    enum FieldKeys: String, CodingKey {
        case sectionIndex, sectionTitle, questionName, sectionText
    }
}
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • Without more information regarding the implementation of `Utilities.userType` one can only provide a speculative solution. However, you probably shouldn't be declaring `contentTypeId` as a `static var`. Swift implements `static` as a lazily initialized one-time value, where the `var` decl means it may be reassigned in the future. Hence, after the first point of use `contentTypeId` will always be the initial value unless you change it at some other point. Perhaps `Utilities.userType` is `.normalUser` when first called. – DaveAMoore Sep 06 '19 at 03:45
  • Here the contentTypeId is a part of protocol of EntryDecodable given as part of the Contentful library, so we can't change its type. – Francis F Sep 06 '19 at 04:48
  • then create a another static variable (e.g, _contentTypeId) with an optional String type. Use that as backing for contentTypeId, then for it’s getter return the appropriate user-based ID if the backing variable is nil. The setter should set the backing variable directly. Let me know if you need an example of what this would look like. – DaveAMoore Sep 06 '19 at 11:53
  • Can't you make two functions `getUserFAQs`and `getGuestFAQs` and set each of them up with the according contentTypeId? And then depending on your user type you call either the one or the other function? – Boommeister Feb 10 '21 at 21:54

0 Answers0