1

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

Antonio Labra
  • 1,694
  • 2
  • 12
  • 21

1 Answers1

0

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
       }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • it's not using my method :c. I totally agree what you said but it's not starting when "Appear". I'm going to update to explain you better – Antonio Labra Apr 30 '20 at 17:36
  • it's seems my list is nil and gets error. I don't really know how to fix it – Antonio Labra Apr 30 '20 at 18:05
  • 1
    @AntonioLabra, try `ForEach(invitationsList ?? [], id: \.self)...` or put entire list in condition `if invitationsList != nil`, because really until response there is nothing to show. – Asperi Apr 30 '20 at 18:10