HomeiOS Developmentios - Data from API not displaying

ios – Data from API not displaying


I’m making an iOS app with my teams web page the place it exhibits all of the teams that exist. That is my teams web page code:

struct MyGroups: View {
    @StateObject personal var vm = GroupsListViewModel()
    var physique: some View {
        Checklist(vm.teams) { group in
            Textual content(group.groupName)
            Textual content("Variety of teams: (vm.teams.rely)")
        }.activity {
            await vm.fetchAllGroups()
            
        }
    }
}

Nevertheless, after I open the web page all I see is that this:

enter image description here

That is my fetchAllGroups perform code:

func fetchAllGroups() async {
        do {
            if let url = Constants.Urls.allGroups {
                let teams = strive await WebService().getCarpoolGroups(url: url)
                print("Teams fetched efficiently: (teams)")
                DispatchQueue.predominant.async {
                    self.teams = teams.map(GroupViewModel.init)
                }
            } else {
                print("Error fetching all teams: URL is nil")
            }
        } catch {
            print("Error fetching all teams: (error)")
        }
    }

That is my api connection code:

import Basis

func fetchDataFromAPI(completion: @escaping ([GroupT]?) -> Void) {
    // Outline the API endpoint URL
    let apiUrl = URL(string: "http://localhost/api/hi")!

    // Create a URLSession
    let session = URLSession.shared

    // Create a URLRequest
    var request = URLRequest(url: apiUrl)
    request.httpMethod = "GET"
    request.addValue("utility/json", forHTTPHeaderField: "Content material-Sort")

    // Make the request
    let activity = session.dataTask(with: request) { knowledge, response, error in
        // Verify for errors
        if let error = error {
            print("Error: (error)")
            completion(nil)
            return
        }

        // Verify for a profitable HTTP response
        guard let httpResponse = response as? HTTPURLResponse, (200...299).incorporates(httpResponse.statusCode) else {
            print("Error: Invalid HTTP response")
            completion(nil)
            return
        }

        // Verify if there may be knowledge
        guard let knowledge = knowledge else {
            print("Error: No knowledge obtained")
            completion(nil)
            return
        }

        // Parse the JSON knowledge
        do {
            let decoder = JSONDecoder()
            let carpools = strive decoder.decode([GroupT].self, from: knowledge)
            completion(carpools)
        } catch {
            print("Error decoding JSON: (error)")
            completion(nil)
        }
    }

    // Resume the duty to make the request
    activity.resume()
}

// Instance utilization:
/*fetchDataFromAPI { carpools in
    if let carpools = carpools {
        // Use 'carpools' in your Swift code
        print("Carpools: (carpools)")
    } else {
        print("Did not fetch knowledge from API")
    }
}*/

Lastly that is my webservice code:

import Basis
import FirebaseAuth
import FirebaseFirestore
import FirebaseFirestoreSwift

class WebService {
    
    // Perform to get carpool group particulars
    func getCarpoolGroups(url: URL) async throws -> [GroupT] {
        let request = URLRequest(url: url)
        
        let (knowledge, response) = strive await URLSession.shared.knowledge(from: url)
        
        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
            throw NetworkError.invalidRequest
        }
        let teams = strive? JSONDecoder().decode([GroupT].self, from: knowledge)
        return teams ?? []
    }
}

I do know I’ve despatched a number of code however I’ve no clue the place the error is coming from. I’ve tried many print statements and none of them are going by means of. Any assist is way appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments