【问题标题】:jsonDecode DecodingError when trying to read feed尝试读取提要时出现 jsonDecode DecodingError
【发布时间】:2019-08-19 18:20:17
【问题描述】:

我正在尝试将 getstream 集成到我的应用程序中,我需要一些帮助来解决我遇到的 jsonDecode 错误。

以下是我采取的步骤: 1.手动创建一个新的订阅组allevents

2.我的后端能够在所有事件中创建一个名为 bday_11 的新提要。

  1. 我从后端创建了 2 个用户 ameetramya 并让他们添加活动。然后使用follow api,我让bday_11同时关注ameetramya

  2. 当我手动执行 bday_11.get() 时,我能够看到预期的活动

In [86]: bday_11.get()
Out[86]:
{u'duration': u'22.46ms',
 u'next': '',
 u'results': [{u'actor': u'chris',
   u'foreign_id': u'picture:10',
   u'id': u'a0febe54-bfad-11e9-9361-0a286b200b2e',
   u'message': u'Beautiful bird!',
   u'object': u'picture:10',
   u'origin': u'user:ameet',
   u'target': '',
   u'time': datetime.datetime(2019, 8, 15, 22, 40, 6, 280764),
   u'verb': u'add'},
  {u'actor': u'chris',
   u'foreign_id': u'picture:10',
   u'id': u'96919914-bfad-11e9-933c-0a286b200b2e',
   u'message': u'Beautiful bird!',
   u'object': u'picture:10',
   u'origin': u'user:ramya',
   u'target': '',
   u'time': datetime.datetime(2019, 8, 15, 22, 39, 48, 788252),
   u'verb': u'add'}]}

现在在客户端,这就是我在 AppDelegate 上初始化客户端的方式。这是使用我们的 Feed 参数对您现有的演示项目进行的修改

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // Setup Stream Client.

    Client.config = .init(apiKey: "ehs4s58m3u43",appId: "57260",token:FEED_TOKEN)

//Client.config = .init(apiKey:apiKey, appID:appID,token:FeedToken)

    // Setup Stream user.

    Client.shared.getCurrentUser(typeOf: GetStreamActivityFeed.User.self) { result in

        // Current user is ready. Load timeline feed.

        if result.error == nil, let viewController = self.window?.rootViewController as? ViewController {

            viewController.reloadData()

        }

    }



    return true

}

这是我尝试阅读创建的提要的方式。

override func viewDidLoad() {

    // Setup a timeline feed presenter.



    let timelineFlatFeed = Client.shared.flatFeed(feedSlug: "allevents",userId: "bday_11")

    presenter = FlatFeedPresenter<GetStreamActivityFeed.Activity>(flatFeed: timelineFlatFeed,

                                                                  reactionTypes: [.likes, .comments])





    super.viewDidLoad()

    setupTextToolBar()

    subscribeForUpdates()

}

我似乎在演示者中收到以下错误:

❌ jsonDecode("The data couldn’t be read because it isn’t in the correct format.", Optional(Swift.DecodingError.typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: 
[CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), 
CodingKeys(stringValue: "actor", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))), 667 bytes)

有人可以帮忙吗?

【问题讨论】:

  • 看起来您的响应 JSON 无效。它实际上是在键和值之前返回 'u' 字符吗?另外,你能分享你解码 JSON 的结构吗?

标签: ios getstream-io


【解决方案1】:

Stream Feeds 有一个名为 Enrichment 的功能(请查看文档here),您可以在其中保持数据规范化并使用对对象的引用而不是活动中的完整对象。在 iOS 客户端上,它默认启用,Activity 默认使用actor 作为User 对象,因此对活动的请求需要actor 键的JSON 对象,但它得到了字符串u'actor': u'chris'。因此,如果您将 actor 用作 User 对象,则解析也将起作用。另一种方法是禁用丰富并使用自定义 Activity 与 actor 作为字符串: typealias MyActivity = EnrichedActivity&lt;String, String, DefaultReaction&gt;

presenter = FlatFeedPresenter<MyActivity>(flatFeed: timelineFlatFeed,
                                          reactionTypes: [.likes, .comments])

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2019-08-08
    • 2021-08-20
    相关资源
    最近更新 更多