0

So I'm using Appwrite as a backend for my application.

In my repository, I want to create a function along the lines of

func listenToAltersForUser(userId: String) -> PassthroughSubject(?) {
     subscription = appwriteClient.realtime(....) { event in 
         if (event.isForUser) {
             sink.send(event)
         }
     }
} 

So all the examples I can find on the internet use traditional networking calls with pre-built extensions to convert a request to a stream. However I'm not making networking calls directly.

I have a function that produces a callback on updates, and I have no idea how to create a subscription that can be listened to. I don't want to create a global passthrough subject for the class, because there could be multiple, and if the userId changes than I need to create a new subscription.

I know in kotlin I'd use callBackFlow but I'm newer to swift.

Cate Daniel
  • 724
  • 2
  • 14
  • 30
  • Why is Combine needed in this story at all? If you really intend to use Combine, what you want to produce here is a Future. – matt Jun 07 '23 at 18:09
  • So I'm looking for a way to listen to the subscription, and progatate events through the layers to the UI. Future is just an async alternative no? The subscription comes through at random times with data updates. – Cate Daniel Jun 07 '23 at 18:21
  • I stand by what I said. – matt Jun 07 '23 at 18:56
  • 1
    @matt a Future only publishes a single value. I believe that the `realtime` function will invoke the closure every time there is a change to the remote object, so the probably do want to return a `PassthroughSubject` – Paulw11 Jun 08 '23 at 02:26

1 Answers1

0

From looking at the AppWrite documentation, it seems like you're registering for real-time notifications for a server and you want to transcribe them to an internal message stream. I think what you are looking to do is something like:

(I typed this code into SO so it may not compile)

func eventsForUser(userId: String) -> PassthroughSubject<[whatever the type for "event" is], Never> {

     let subject = PassthroughSubject<[whatever the type for event is], Never>()
     net_subscription = appwriteClient.realtime(....) { event in 
         if (event.isForUser) {
             subject.send(event)
         }
     }

     return subject

} 

This function creates a pipeline (the passthrough subject) that echoes the events that come in from the server. The values that pass through the pipeline end up being the same events that appwriteClient.realtime delivers to the callback.

If other code wants to listen to that pipeline:

   // Set up a pipeline for the user once
   let userEvents = eventsForUser(userId: aUserID)

   ... and then later every one that wants to subscribe will use...

   let subscription = userEvents.sink { event in
     ... do something with event
   }

After you've created the PassthroughSubject others can subscribe to it and listen for things that interest them.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34