Tio
Tio

Reputation: 1036

How to hit an API call using a method

I'm currently developing an application using SwiftUI.

I want to hit an API when I tap a button.

But my code doesn't work. (*I can check I receive a value when I hit an API URL using a console.)

How can I use load() method when I tap a button in ShowAPI struct?


Here are the codes:

ContentView.swift

import SwiftUI

struct ContentView: View {
    @ObservedObject var hitAPI = HitAPI()

    var body: some View {

    ShowAPI()
        .environmentObject(hitAPI)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ShowAPI.swift

import SwiftUI

struct ShowAPI: View {
    
    @EnvironmentObject var hitAPI: HitAPI
    
    var body: some View {
        Text(hitAPI.infos?.name ?? "")
   
        Button(action: {
            HitAPI().load()
        }, label: {
            Text("GET")
        })
    }
}

struct ShowAPI_Previews: PreviewProvider {
    static var previews: some View {
        ShowAPI()
    }
}

HitAPI.swift

import Foundation
import Combine

class HitAPI: ObservableObject {
    
    @Published var infos: Infos?
    
//        init() {
//            load()
//        }
    
    func load() {

        guard let urlStr = "https://sample.com/api/test/1/"
                .addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
        else {
            fatalError("URL String Error")
        }
        
        guard let url = URL(string: urlStr) else {
            fatalError("URL convert Error")
        }
        
        let task = URLSession.shared.dataTask(with: url) { data, response, error in
            if data != nil{
                DispatchQueue.main.async {
                    self.infos = try! JSONDecoder().decode(Infos.self, from: data!)
                    print(self.infos!.name)//I can check a value in a console
                }
            }else{
                fatalError("JSON Data decode Error")
            }
        }
        task.resume()
        
    }
}

struct Infos: Codable {
    var name: String
}

If I use load() method in init(), Text(hitAPI.infos?.name ?? "") shows an value


Xcode: Version 12.0.1

Upvotes: 0

Views: 825

Answers (1)

Sam
Sam

Reputation: 2446

You're creating a new HitAPI every time you call load, which doesn't give it enough time to finish the request before being deallocated:

import SwiftUI

struct ShowAPI: View {
    
    @EnvironmentObject var hitAPI: HitAPI
    
    var body: some View {
        Text(hitAPI.infos?.name ?? "")
   
        Button(action: {
            HitAPI().load()
        }, label: {
            Text("GET")
        })
    }
}

struct ShowAPI_Previews: PreviewProvider {
    static var previews: some View {
        ShowAPI()
    }
}

Change it to this:


struct ShowAPI: View {
    
    @EnvironmentObject var hitAPI: HitAPI
    
    var body: some View {
        Text(hitAPI.infos?.name ?? "")
   
        Button(action: {
            hitAPI.load()
        }, label: {
            Text("GET")
        })
    }
}

struct ShowAPI_Previews: PreviewProvider {
    static var previews: some View {
        ShowAPI()
    }
}

Upvotes: 1

Related Questions