세상을 더 편리하게
article thumbnail
[iOS / SwiftUI] Paging 무한 스크롤
Swift/Apple Framework 2023. 5. 23. 02:23

0. 문제 사진을 비동기적으로 불러와야 할 때 다음 URL 요청을 언제 해야하는지 관건이다. 해결방안 1. 가장 먼저 생각나는 것은 스크롤 위치였다. - 스크롤 위치에 따른 업로드 였다. 하지만 SwiftUI에서는 스크롤 위치를 알기엔 너무 많은 불필요 요쇼들이 필요하다. 2. 80% 전체 사진 중 80% 정도 업로드가 완료 되었을 때 이미지를 불러오자. 전체 불러올 수 있는 이미지 중에서 80% 정도 로딩이 되었을 때 다음 URL을 요청하는 방법이다. (2)번이 사실 가장 깔끔한 답안이다.

article thumbnail
[iOS / SwiftUI] Combine - URLSession
Swift/Apple Framework 2023. 5. 8. 17:29

이번 장에서는 Combine의 실제 사용 사례를 중심으로 알아보고자 한다. 사실 Combine에서 자주 쓰이는 부분은 URLSession일 것 같다. URLSession With Combine // 애플 공식 문서 예시 코드 살짝 수정 struct User: Codable { let name: String let userID: String } let url = URL(string: "https://example.com/endpoint")! cancellable = urlSession .dataTaskPublisher(for: url) .tryMap() { element -> Data in guard let httpResponse = element.response as? HTTPURLResponse, ht..

article thumbnail
[iOS / SwiftUI] Combine - AnyCancellable
Swift/Apple Framework 2023. 5. 8. 04:59

AnyCancellable import Combine import SwiftUI struct CombineView: View { var vm = CombineViewModel() @State var anyCancellable = Set() @State var number = 0 var body: some View { VStack { Text("\(number)") .padding() Button(action: { vm.number.send(Int.random(in: 1...1000)) // # 수정됨 // print(vm.number) }, label: { Text("Button") }) Button(action: { anyCancellable.first?.cancel() }) { Text("Cancel..

article thumbnail
[iOS / SwiftUI] Combine - CurrentValueSubject, PassthroughSubject
Swift/Apple Framework 2023. 5. 2. 17:29

저번 글에서 가볍게 Combine을 맛봤다. 이제는 실전으로 이것저것 써보자 Combine에서 쉽게 Publisher를 만드는 방법 중 하나는 CurrentValueSubject와 PassthroughSubject 이다. CurrentValueSubjcet Single Value를 감싸고 있는 Subject로써 Value 값이 변화면 Publish 한다! import Combine import SwiftUI struct CombineView: View { var vm = CombineViewModel() @State var anyCancellable = Set() @State var number = 0 var body: some View { VStack { Text("\(number)") .padding..