当前位置:网站首页>Swiftui network request and data loading
Swiftui network request and data loading
2022-06-09 00:01:00 【Loneliness under fireworks】
Preface
SwiftUI It's just UI, Network requests and Swift It's all the same , As for the network request encapsulation, I will not repeat it one by one , If you want to see it, please see swift Use Moya Make a network request
Let me talk about the network request method , I am also a beginner , There are all kinds of loose places. Please leave a message , I will also add slowly in the follow-up study ,
1 List Data loading
Model This and Swift There's no difference , I won't let it go
Only one load is done here
1、ViewModel Code
- @Published Modifier attribute inform Swift Keep an eye on this variable . If anything changes , All views that use this variable body Will be updated .
- We assign the data received from the network request to model attribute , This will trigger us to step 1 The actions mentioned in .
- init Execute once every time , If in tabBar page , Please be there. init Set limits in , Otherwise, each switch will be executed init Code
// Here I encapsulate the network request component
import YLNetTool
class YLStudyVM: ObservableObject {
@Published var model: YLStudyModel? = nil
init() {
getList()
}
func getList() {
YLNetTool.request(target: YLStudyApi.study_list,
modelType: YLStudyModel.self) {
[weak self]code, model in
self?.model = model
print("666")
} failureBlock: {
code, msg in
print("???")
}
}
}
2、row In the code
This is mainly to add model, Personal feelings and Swift There's not much difference in , Negligible
struct YLStudyRow: View {
var model: YLStudyModel.resultModel? = nil
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text(model?.title ?? "")
.font(.system(size: 14, weight: .medium))
.padding(EdgeInsets(top: 10, leading: 15, bottom: 0, trailing: 10))
Text(" Last learned : Do you know where your money went ?")
.foregroundColor(RGB(102))
.font(.system(size: 13, weight: .regular))
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
HStack {
Text(" Have learned :0%")
.foregroundColor(.red)
.font(.system(size: 12, weight: .regular))
Spacer()
Text(" Enter learning ")
.foregroundColor(.white)
.font(.system(size: 12, weight: .medium))
.frame(width: 70, height: 24)
.background(
LinearGradient(gradient: Gradient(colors:[RGB(255, 101, 86), RGB(249, 0, 0)]),
startPoint: .top,
endPoint: .bottom
)
)
.cornerRadius(12)
}
.padding(EdgeInsets(top: 16, leading: 10, bottom: 0, trailing: 10))
}
.padding(EdgeInsets(top: 0, leading: 10, bottom: 10, trailing: 10))
.background(Color.white)
.cornerRadius(10)
}
}
struct YLStudyRow_Previews: PreviewProvider {
static var previews: some View {
Group {
YLStudyRow()
.previewLayout(.fixed(width: 414, height: 120))
}
}
}
3、 Main page get network request 、 Refresh
struct YLStudy: View {
@ObservedObject var viewModel = YLStudyVM()
init() {
UITableView.appearance().backgroundColor = .clear
}
var body: some View {
List() {
Section(header: YLStudyHeader()) {
ForEach(0 ..< (viewModel.model?.getUserBuyMicroCourseResults.count ?? 0)) { index in
YLStudyRow(model: viewModel.model?.getUserBuyMicroCourseResults[index])
.listRowSeparator(.hidden)
}
}
}
.listStyle(.grouped)
.background(RGB(245))
.navigationTitle(" Study ")
.navigationBarTitleDisplayMode(.inline)
}
}
struct YLStudy_Previews: PreviewProvider {
static var previews: some View {
YLStudy()
}
}
Draw lessons from articles
Use SwiftUI To develop a APP - List view & Network request
边栏推荐
- Is it safe to open an account online? What should we do?
- [问题已解决]-golang sarama报错Tried to send a message to a replica that is not the leader
- Day07/08/09 depth first search and breadth first search
- leetcode开篇打卡:小黑吃回文
- Build step ‘Execute shell’ marked build as failure
- The State Administration of market supervision took the initiative to supervise the 618 e-commerce promotion Festival: what are the criticisms of the e-commerce promotion Festival
- 百鸡问题扩展-N鸡问题
- 哈工大信息安全概论期末复习
- Detailed explanation of conv2d parameters
- Cloud native technology - Construction of highly available etcd database cluster
猜你喜欢

Online Morse code online translation and conversion tool

工具变量与两阶段最小二乘stata

投资金额降低40% 协鑫能科自研换电产品

CF1505C Fibonacci Words

Constructors and Destructor

Day07/08/09 depth first search and breadth first search
![[arxiv2018] you only look twice: rapid multi scale object detection in satellite image](/img/36/9abf6fe054d1f3e949fdb4f8a62474.png)
[arxiv2018] you only look twice: rapid multi scale object detection in satellite image

Accessing the database in jdbcstamp mode

Wordcloud quick installation and Application

operator
随机推荐
Build step ‘Execute shell’ marked build as failure
Digital IC Design -- Design of asynchronous FIFO
穷举问题-搬砖
Application of slip ring in automatic mask machine
[问题已处理]-harbor可以login成功pull成功但是push报错
vcs && verdi labs(adder8)
.podspec 依赖问题处理
LeetCode-1241. Comments per post
New technology (1): 2021
Leetcode 349 intersection of two arrays *easy*) problem solving analysis
How long will it take for financial products to be cleared?
进程间通信方式(管道、消息队列、内存映射区、共享内存等)
Intimacy supporting pressure lines and trends
Form label
Shutter row & column assembly
Thesis reading | eventmix: an efficient augmentation strategy for event based data
Interprocess communication mode (pipeline, message queue, memory mapping area, shared memory, etc.)
875. Koko Eating Bananas
枚举
Sequence alignment with BWA