1

I have inherited the following code and unfortunately after 5 days of debugging I have not made much progress and I would be forever grateful if anybody could debug it for me, I am totally at a loss on this.

Xcode is producing the following errors:

Cannot find type 'RealtimePostgresChange' in scope

Value of type 'AsyncStream<InsertAction>' has no member 'onReceive'

Code:

import Supabase
import Combine
import SwiftUI
import PostgREST

// 6. Message Service
@MainActor
class MessageService: ObservableObject {
    @Published var messages: [Message] = []
    @Published var error: Error?

    private let supabase: SupabaseClient
    private var channel: RealtimeChannelV2?
    private(set) var currentUserId: UUID?

    init(supabase: SupabaseClient, userId: UUID?) {
        self.supabase = supabase
        self.currentUserId = userId
    }

    func fetchMessages() async {
        do {
            let messages: [Message] = try await supabase
                .from("messages")
                .select()
                .order("created_at", ascending: true)
                .execute()
                .value

            self.messages = messages
        } catch {
            self.error = error
        }
    }

    func sendMessage(content: String) async {
        guard !content.isEmpty, let userId = currentUserId else { return }

        let message = Message(
            id: UUID(),
            content: content,
            user_id: userId,
            created_at: Date(), deleted_at: nil
        )

        do {
            try await supabase
                .from("messages")
                .insert(message)
                .execute()
        } catch {
            self.error = error
        }
    }
    
  
    func subscribeToRealtime() async {
        
        channel = supabase.realtimeV2.channel("messages")

        let changes = channel?.postgresChange(
            
            Realtime.InsertAction.self,
            schema: "public",
            table: "messages"
        )
        
        changes?.onReceive { [weak self] (changes: [RealtimePostgresChange<Message>]) in
            guard let self else { return }
            
            for change in changes {
                let newMessage = change.new
                
                if !self.messages.contains(where: { $0.id == newMessage.id }) {
                    self.messages.append(newMessage)
                }
            }
        }

        Task {
            do {
                try await channel?.subscribe()
            } catch {
                self.error = error
            }
        }
    }
       func updateUserId(_ newId: UUID?) {
        currentUserId = newId
    }
}

I have tried replacing RealtimePostgresChange with RealtimeMessage but that seems to cause several other issues.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.