supabase / supabase-swift

A Swift client for Supabase

Home Page:https://supabase.com/docs/reference/swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the proper way to setup and start realtime? I don't seem to get any feedback on triggers based on the starter code found in the repo.

vikrvm opened this issue · comments

I'm setting up Supabase Realtime in my application and looking through some of the example code I found the following. My problem is now I can't get any triggers to occur. I've ensured that real-time is enabled on this table and that RLS policies all work. Looking for any guidance on this starter code found.

import Observation
import Foundation
import Realtime

@Observable final class FriendsOO {
    
    enum State {
        case idle
        case loading
        case success
        case failed
    }
    
    var state: State = .idle
    
    var friends: [UserProfileModel] = []
    var sentRequests: [FriendModel] = []
    var receivedRequests: [FriendModel] = []
    
    init() {
       Task {
         let channel = await supabaseClient.realtimeV2.channel("public:friends")

         let insertions = await channel.postgresChange(InsertAction.self, table: "friends")
         let updates = await channel.postgresChange(UpdateAction.self, table: "friends")
         let deletions = await channel.postgresChange(DeleteAction.self, table: "friends")

         await channel.subscribe()

         Task {
           for await insertion in insertions {
               print("insertion") // HERE
           }
         }

         Task {
           for await update in updates {
               print("update") // HERE
           }
         }

         Task {
           for await delete in deletions {
               print("delete") // HERE
           }
         }
       }
     }
    
    func getFriendRequests() async -> () {...}
    
    func sendFriendRequest(profileId: String) async -> () {...}
    
    func removeFriend(profileId: String) async -> () {...}
    
    func acceptFriendRequest(requestId: String, profileId: String) async -> () {...}
    
    func declineFriendRequest(requestId: String) async -> () {...}
    
    func removeSentFriendRequest(requestId: String) async -> () {...}
}

Hi @mypalvikram

Your code seems correct, I can't think of why it isn't working just by looking at it.

Do the following, if you don't have a Logger set up, please set up SupabaseLogger and post the logs you get here.

Example of SupabaseLogger implementation: https://github.com/supabase-community/supabase-swift/blob/main/Examples/SlackClone/Logger.swift
Example of where to add the logger: https://github.com/supabase-community/supabase-swift/blob/main/Examples/SlackClone/Supabase.swift#L28

Post the logs here after that.

Hi @mypalvikram

Your code seems correct, I can't think of why it isn't working just by looking at it.

Do the following, if you don't have a Logger set up, please set up SupabaseLogger and post the logs you get here.

Example of SupabaseLogger implementation: https://github.com/supabase-community/supabase-swift/blob/main/Examples/SlackClone/Logger.swift Example of where to add the logger: https://github.com/supabase-community/supabase-swift/blob/main/Examples/SlackClone/Supabase.swift#L28

Post the logs here after that.

I was able to solve my issue. The issue was I was injecting the Observable Object into the environment at a level too high. I needed to instantiate it at the tab bar level and inject it into the environment, giving all my tabs access to that instance. I now get logs in the console correctly.