swiftuicompletionhandlerswift5mutating-function

How to fill a list using Completion Handlers [SWIFT UI]


I want to fill a list using a completion handler, the problem is that it's loading nil in the first execution and marks errors when I try to consume my View where I have my list that it's filled by completion handler... Any suggestions to fix this?

this is how I try to fill my list

let invitationService: InvitationService = InvitationService()
@State private var invitationsList : [Appointment]?

init() {
    let defaults = UserDefaults.standard
    let userId = defaults.integer(forKey: "userId")
    var appointmentList : [Appointment]?

    invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
        if appointment != nil{
            appointmentList = appointment
        }
    })

    _invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
}

as you can see I would need to fill my list until InvitationService Request ends but If I try to put it inside the code I got a

Escaping closure captures mutating 'self' parameter

error

I mean

  invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
            if appointment != nil{
                appointmentList = appointment
                self._invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
            }
        })

My VIEW

var body: some View {
    NavigationView{

    ZStack{
        colors["LightGray"]?.edgesIgnoringSafeArea(.all)
        Text("Hey").onAppear(){
            let defaults = UserDefaults.standard
                                   let userId = defaults.integer(forKey: "userId")

                                   self.invitationService.getInvitations(id: userId) { (appointment) in
                                       self.invitationsList = appointment

                                   }
        }
        List {

            ForEach(invitationsList!, id: \.self){invitation in
                NavigationLink(destination: InvitationDetailView(invitation: invitation)){
                    HStack{
                        VStack(alignment: .center){
                            Text(invitation.startDate.prefix(2))
                                .font(.largeTitle)
                            Text(invitation.startDate[2..<6]).font(.subheadline)

                        }.padding()
                        Spacer().frame(width:UIScreen.main.bounds.width/15, alignment: .leading)

                        ImageView(withURL: invitation.imageProfile,widthValue: 80, heightValue: 80)
                            .aspectRatio(contentMode: .fit)
                            .clipShape(Circle())
                        Spacer()
                        VStack(alignment: .leading){
                            Text(invitation.titleVisit)
                                .font(.headline)
                                .frame(width: UIScreen.main.bounds.width/3, alignment: .leading)
                            Text(invitation.typeVisit)
                                .font(.footnote)
                            Text(invitation.startDate.suffix(9))
                                .font(.caption)
                        }.padding()
                    }
                    .background(self.colors["White"])
                    .cornerRadius(25)
                    .foregroundColor(self.colors["Black"])
                    .shadow(radius: 2, x: 0, y: 0)
                }



            }.onDelete(perform: delete)



        }


    }.hideNavigationBar(text: "back".localized)
}

}

I'm trying to execute and when I do that invitationsList is nil


Solution

  • I recommend do not put such things into View.init, because SwiftUI view is struct, value, and can be re-created several times during layout/rendering. The better approach is to do this after view appeared (or, what is better, outside view hierarchy at all).

    Anyway being in view here is possible approach

    ...
    let invitationService: InvitationService = InvitationService()
    @State private var invitationsList : [Appointment]? = nil
    
    // nothing for init in this case
    
    var body: some View {
        Text("Any subview here")
        .onAppear {
           let defaults = UserDefaults.standard
           let userId = defaults.integer(forKey: "userId")
    
           self.invitationService.getInvitations(id: userId) { (appointment) in
               self.invitationsList = appointment
           }
        }
    }