尝试读取供稿时jsonDecode DecodingError

时间:2019-08-16 16:38:28

标签: ios getstream-io

我试图将getstream集成到我的应用程序中,我需要一些有关我得到的jsonDecode错误的帮助。

这是我采取的步骤: 1.手动创建一个新的供稿组allevents

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

  1. 我从后端创建了两个用户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)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

Stream Feeds具有一个名为“充实”的功能(请检查文档here),您可以在其中保持数据规范化,并使用对对象的引用,而不是活动中的完整对象。在iOS客户端上,默认情况下启用了该功能,默认情况下,Activity使用actor作为User对象,因此对于要求使用actor键的JSON对象的活动的请求字符串u'actor': u'chris'。因此,如果将actor用作User对象,则解析也将起作用。另一种方法是禁用扩展,并使用以actor作为字符串的自定义Activitytypealias MyActivity = EnrichedActivity<String, String, DefaultReaction>

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