GetStream / swift-activity-feed

Stream Swift iOS Activity Feed Components
https://getstream.io/
BSD 3-Clause "New" or "Revised" License
30 stars 17 forks source link

Get Completion isn't working #13

Closed Veeit closed 3 years ago

Veeit commented 3 years ago

Hey Guys,

I'm currently trying to implement getStream with SwiftUI. But I'm experience a wired behaviour of feed.get(). The completion isn't running. But I get the response printed inside the console, with mean that I get the correct response from the backend.

My Code:

Client.config = .init(apiKey: "apiKey-is-hidden",
                                  appId: "97497",  logsEnabled: true)

            Client.shared.setupUser(token: "token-is-hidden", completion: { result in
                    guard result.error == nil else {
                        preconditionFailure("Cannot setup user: \(result.error!)")
                    }

                if let feedId = FeedId(feedSlug: "site") {
                    let timelineFlatFeed = Client.shared.flatFeed(feedId)
                    print("time line: ==== \(timelineFlatFeed)")

                    let seconds = 4.0
                    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
                        timelineFlatFeed.get(enrich: true, pagination: .limit(2)) { response in
                            if (result.error != nil) {
                                print("error")
                            } else {
                                print("result")
                            }
                        }
                    }
                }

            })

My console log: https://pastebin.com/E82PPWvR

b-onc commented 3 years ago

Hello @Veeit ,

You need to hold a strong reference to timelineFlatFeed.

Try this:


Client.config = .init(apiKey: "apiKey-is-hidden",
                                  appId: "97497",  logsEnabled: true)

let timelineFlatFeed = Client.shared.flatFeed(feedId)

            Client.shared.setupUser(token: "token-is-hidden", completion: { result in
                    guard result.error == nil else {
                        preconditionFailure("Cannot setup user: \(result.error!)")
                    }

                if let feedId = FeedId(feedSlug: "site") {
                    let timelineFlatFeed = Client.shared.flatFeed(feedId)
                    print("time line: ==== \(timelineFlatFeed)")

                    let seconds = 4.0
                    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
                        timelineFlatFeed.get(enrich: true, pagination: .limit(2)) { response in
                            if (result.error != nil) {
                                print("error")
                            } else {
                                print("result")
                            }
                        }
                    }
                }

            })
Veeit commented 3 years ago

Hello @b-onc ,

same result. I tried this code now:

Client.config = .init(apiKey: "ztyjtywyeb28",
                                  appId: "97497",  logsEnabled: true)

            Client.shared.setupUser(token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiLTEifQ.4ttgLiot0zNIy1YOqENZz3YfsCLU3oej9n7l-m5Vcrw", completion: { result in
                    guard result.error == nil else {
                        preconditionFailure("Cannot setup user: \(result.error!)")
                    }
            })

            let timelineFlatFeed = Client.shared.flatFeed(feedSlug: "site")!

            print("time line: ==== \(timelineFlatFeed)")

            timelineFlatFeed.get(pagination: .limit(2)) { response in
                print("===========")
                print("===========")
                print("===========")
                print("===========")
                print("===========")

                if (response.error != nil) {
                    print("error")
                } else {
                    print("result")
                }

                print("===========")
                print("===========")
                print("===========")
                print("===========")
                print("===========")
            }

and got his console log:

๐Ÿ“ฐ  Stream Feed v.1.0
๐Ÿ”‘  ztyjtywyeb28
๐Ÿ”—  https://api.stream-io-api.com/api/v1.0/
๐Ÿด [03/11 19:05:07.612] ๐Ÿงน Disconnect. Reset Client User, Token, URLSession and WebSocket. Old user id: <Empty>.
๐Ÿด [03/11 19:05:07.616] ๐Ÿงน Disconnect. Reset Client User, Token, URLSession and WebSocket. Old user id: -1.
๐Ÿ‘ค  User id: -1
๐Ÿ€„๏ธ  Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiLTEifQ.4ttgLiot0zNIy1YOqENZz3YfsCLU3oej9n7l-m5Vcrw
๐Ÿด [03/11 19:05:07.617] โžก๏ธ create(GetStream.User, true)
๐Ÿด [03/11 19:05:07.617] ๐Ÿงพ Request Data:
{
  "id" : "-1"
}
๐Ÿด [03/11 19:05:07.617] ๐Ÿงพ URL parameters: ["get_or_create": true]
time line: ==== site:-1
๐Ÿด [03/11 19:05:07.623] โžก๏ธ get(site:-1, true, GetStream.Pagination.limit(2), "", GetStream.FeedMarkOption.none, GetStream.FeedReactionsOptions(rawValue: 0))
๐Ÿด [03/11 19:05:07.623] ๐Ÿงพ Parameters: ["limit": 2]
2020-11-03 19:05:07.749395+0100 TheStream[2736:110703] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
๐Ÿด [03/11 19:05:08.353] โฌ…๏ธ Response 201: https://api.stream-io-api.com/api/v1.0/user/?api_key=ztyjtywyeb28&get_or_create=1
๐Ÿด [03/11 19:05:08.353] โ’ฟโ“ˆโ“„โ“ƒ 144 bytes
 {
  "duration" : "0.84ms",
  "id" : "-1",
  "data" : {
    "name" : "Unknown"
  },
  "updated_at" : "2020-10-25T15:09:07.295895Z",
  "created_at" : "2020-10-25T15:09:07.295895Z"
}
๐Ÿด [03/11 19:05:08.357] ๐Ÿ‘ค The current user was setupped with id: -1
๐Ÿด [03/11 19:05:08.364] โฌ…๏ธ Response 200: https://api.stream-io-api.com/api/v1.0/enrich/feed/site/-1/?api_key=ztyjtywyeb28&limit=2
๐Ÿด [03/11 19:05:08.365] โ’ฟโ“ˆโ“„โ“ƒ 6368 bytes
 {
  "results" : [
    {
      "id" : "ab63aa9c-1dfe-11eb-8080-80017832bb19",
      "to" : [
        "user:6764",
        "tasks:6764"
      ],
      "verb" : "complete",
      "time" : "2020-11-03T18:01:59.926646",
      "object_type" : "task",
      "object" : {
        "collection" : "enrichment_store",
        "id" : "projects-Task-313718",
        "data" : {
          "description" : null,
          "comment_count" : 0,
          "id" : 313718,
          "video" : null,
          "project_set" : [
            {
              "id" : 14937,
              "private" : false,
              "name" : "tindericebreakers",
              "user" : 6764
            }
          ],
          "created_at" : "2020-11-03T18:01:59.926646Z",
          "event" : null,
          "due_at" : null,
          "done" : true,
          "in_progress" : false,
          "product_set" : [

          ],
          "og_image" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/tasks\/2020\/11\/03\/bcf2c8b2-335b-4941-b71e-dbabe2ee3971.jpg",
          "attachment" : null,
          "praise" : 0,
          "done_at" : "2020-11-03T18:01:59.921280Z",
          "updated_at" : "2020-11-03T18:04:15.707341Z",
          "user" : {
            "github_handle" : "AndreiChiha",
            "hardcore_mode" : false,
            "private" : false,
            "last_name" : "Chihalau",
            "shipstreams_handle" : "",
            "donor" : false,
            "id" : 6764,
            "instagram_handle" : "",
            "week_tda" : 2,
            "nomadlist_handle" : "",
            "description" : "Front-End Intern at Kappa.London",
            "gold" : false,
            "email_notifications" : true,
            "interests" : [

            ],
            "website" : "",
            "digest" : true,
            "twitter_handle" : "AChihalau",
            "maker_score" : 51,
            "accent" : "#00a676",
            "bmc_handle" : "",
            "dark_mode" : false,
            "date_joined" : "2020-10-12T15:18:39.515474Z",
            "is_live" : false,
            "status" : null,
            "avatar" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/avatars\/2020\/10\/27\/kappa_logo_2.png",
            "username" : "AChihalau",
            "weekends_off" : false,
            "first_name" : "Andrei",
            "streak" : 1,
            "tester" : false,
            "product_hunt_handle" : "@andrei_chihalau",
            "is_staff" : false,
            "og_image" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/og\/2020\/11\/03\/ed5b965a-f94a-45a3-91a5-1cea859575a1.jpg",
            "header" : null,
            "telegram_handle" : "",
            "verified" : false,
            "timezone" : "Europe\/Bucharest"
          },
          "content" : "Test the \"Saved Messages\" functionality +minor desigh changes #tindericebreakers"
        },
        "foreign_id" : "enrichment_store:projects-Task-313718",
        "updated_at" : "2020-11-03T18:04:16.266444Z",
        "created_at" : "2020-11-03T18:02:00.958077Z"
      },
      "actor" : {
        "collection" : "enrichment_store",
        "id" : "accounts-User-6764",
        "data" : {
          "github_handle" : "AndreiChiha",
          "hardcore_mode" : false,
          "private" : false,
          "last_name" : "Chihalau",
          "shipstreams_handle" : "",
          "donor" : false,
          "id" : 6764,
          "instagram_handle" : "",
          "week_tda" : 2,
          "nomadlist_handle" : "",
          "description" : "Front-End Intern at Kappa.London",
          "gold" : false,
          "email_notifications" : true,
          "interests" : [

          ],
          "website" : "",
          "digest" : true,
          "twitter_handle" : "AChihalau",
          "maker_score" : 51,
          "accent" : "#00a676",
          "bmc_handle" : "",
          "dark_mode" : false,
          "date_joined" : "2020-10-12T15:18:39.515474Z",
          "is_live" : false,
          "status" : null,
          "avatar" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/avatars\/2020\/10\/27\/kappa_logo_2.png",
          "username" : "AChihalau",
          "weekends_off" : false,
          "first_name" : "Andrei",
          "streak" : 1,
          "tester" : false,
          "product_hunt_handle" : "@andrei_chihalau",
          "is_staff" : false,
          "og_image" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/og\/2020\/11\/03\/ed5b965a-f94a-45a3-91a5-1cea859575a1.jpg",
          "header" : null,
          "telegram_handle" : "",
          "verified" : false,
          "timezone" : "Europe\/Bucharest"
        },
        "foreign_id" : "enrichment_store:accounts-User-6764",
        "updated_at" : "2020-11-03T18:04:16.209898Z",
        "created_at" : "2020-10-29T07:35:30.837139Z"
      },
      "origin" : null,
      "target" : "",
      "actor_type" : "user",
      "target_type" : null,
      "foreign_id" : "projects.Task:313718"
    },
    {
      "id" : "a5c8aae2-1dfe-11eb-8080-80000a76d4ff",
      "to" : [
        "user:6764",
        "tasks:6764"
      ],
      "verb" : "complete",
      "time" : "2020-11-03T18:01:50.522237",
      "object_type" : "task",
      "object" : {
        "collection" : "enrichment_store",
        "id" : "projects-Task-313717",
        "data" : {
          "description" : null,
          "comment_count" : 0,
          "id" : 313717,
          "video" : null,
          "project_set" : [
            {
              "id" : 14937,
              "private" : false,
              "name" : "tindericebreakers",
              "user" : 6764
            }
          ],
          "created_at" : "2020-11-03T18:01:50.522237Z",
          "event" : null,
          "due_at" : null,
          "done" : true,
          "in_progress" : false,
          "product_set" : [

          ],
          "og_image" : null,
          "attachment" : null,
          "praise" : 0,
          "done_at" : "2020-11-03T18:01:50.514718Z",
          "updated_at" : "2020-11-03T18:01:50.522259Z",
          "user" : {
            "github_handle" : "AndreiChiha",
            "hardcore_mode" : false,
            "private" : false,
            "last_name" : "Chihalau",
            "shipstreams_handle" : "",
            "donor" : false,
            "id" : 6764,
            "instagram_handle" : "",
            "week_tda" : 2,
            "nomadlist_handle" : "",
            "description" : "Front-End Intern at Kappa.London",
            "gold" : false,
            "email_notifications" : true,
            "interests" : [

            ],
            "website" : "",
            "digest" : true,
            "twitter_handle" : "AChihalau",
            "maker_score" : 53,
            "accent" : "#00a676",
            "bmc_handle" : "",
            "dark_mode" : false,
            "date_joined" : "2020-10-12T15:18:39.515474Z",
            "is_live" : false,
            "status" : null,
            "avatar" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/avatars\/2020\/10\/27\/kappa_logo_2.png",
            "username" : "AChihalau",
            "weekends_off" : false,
            "first_name" : "Andrei",
            "streak" : 1,
            "tester" : false,
            "product_hunt_handle" : "@andrei_chihalau",
            "is_staff" : false,
            "og_image" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/og\/2020\/11\/02\/b35cc84a-9c3e-4d6c-8d3a-7a57b2ec5dcb.jpg",
            "header" : null,
            "telegram_handle" : "",
            "verified" : false,
            "timezone" : "Europe\/Bucharest"
          },
          "content" : "Refactor \"Saved Messages\" storage functionality #tindericebreakers"
        },
        "foreign_id" : "enrichment_store:projects-Task-313717",
        "updated_at" : "2020-11-03T18:01:51.202058Z",
        "created_at" : "2020-11-03T18:01:51.202058Z"
      },
      "actor" : {
        "collection" : "enrichment_store",
        "id" : "accounts-User-6764",
        "data" : {
          "github_handle" : "AndreiChiha",
          "hardcore_mode" : false,
          "private" : false,
          "last_name" : "Chihalau",
          "shipstreams_handle" : "",
          "donor" : false,
          "id" : 6764,
          "instagram_handle" : "",
          "week_tda" : 2,
          "nomadlist_handle" : "",
          "description" : "Front-End Intern at Kappa.London",
          "gold" : false,
          "email_notifications" : true,
          "interests" : [

          ],
          "website" : "",
          "digest" : true,
          "twitter_handle" : "AChihalau",
          "maker_score" : 51,
          "accent" : "#00a676",
          "bmc_handle" : "",
          "dark_mode" : false,
          "date_joined" : "2020-10-12T15:18:39.515474Z",
          "is_live" : false,
          "status" : null,
          "avatar" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/avatars\/2020\/10\/27\/kappa_logo_2.png",
          "username" : "AChihalau",
          "weekends_off" : false,
          "first_name" : "Andrei",
          "streak" : 1,
          "tester" : false,
          "product_hunt_handle" : "@andrei_chihalau",
          "is_staff" : false,
          "og_image" : "https:\/\/ik.imagekit.io\/makerlog\/media\/uploads\/og\/2020\/11\/03\/ed5b965a-f94a-45a3-91a5-1cea859575a1.jpg",
          "header" : null,
          "telegram_handle" : "",
          "verified" : false,
          "timezone" : "Europe\/Bucharest"
        },
        "foreign_id" : "enrichment_store:accounts-User-6764",
        "updated_at" : "2020-11-03T18:04:16.209898Z",
        "created_at" : "2020-10-29T07:35:30.837139Z"
      },
      "origin" : null,
      "target" : "",
      "actor_type" : "user",
      "target_type" : null,
      "foreign_id" : "projects.Task:313717"
    }
  ],
  "next" : "\/api\/v1.0\/enrich\/feed\/site\/-1\/?api_key=ztyjtywyeb28&id_lt=a5c8aae2-1dfe-11eb-8080-80000a76d4ff&limit=2",
  "duration" : "13.18ms"
}

the completion handler of .get is still not working.

matteing commented 3 years ago

Facing this issue too. Any help?

b-onc commented 3 years ago

Hello @Veeit @matteing ,

You need to hold a strong reference to the feed object for its completion to be called. If you're holding a strong reference but still seeing this issue, can you share a sample project?

Veeit commented 3 years ago

ios.zip

I don't know where you see a weak reference.

b-onc commented 3 years ago

Hello @Veeit Thanks for the sample project. There are a couple of issues with it:

  1. Client configuration must be done once, on app start, preferably in scene(_,willConnectTo:)
  2. You should wait for the result of setupUser to call feed.get
  3. In the project you've shared, timelineFlatFeed is a local variable, which is deallocated after the lifetime of the closure its in (onAppear(perform:)). You should hold a reference to the timelineFlatFeed so it's not deallocated until gets completion is ran.

I've made these changes and I can see that get completion runs as expected.

b-onc commented 3 years ago

Closing due to inactivity