SwiftUI - 如何使用列表单元格内的异步操作更改同步值 [英] SwiftUI - How do I change a synchronous value using an asynchronous operation inside a List Cell

查看:21
本文介绍了SwiftUI - 如何使用列表单元格内的异步操作更改同步值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 上一题正确回答.这种情况是界面中某处的图像.

I have this previous question properly answer. That case is of an image somewhere in the interface.

我有相同问题的另一个变体,但现在图像位于列表单元格内.

I have another variation of the same problem but now the image is inside a List Cell.

该图片显示了一个挂锁,只有在 SwiftUI 上未购买特定的应用内购买时才会显示该挂锁.

That image shows a padlock that must only show if a particular inapp purchase was not purchased, on SwiftUI.

类似的东西

  Image(systemName: "lock.circle.fill")
    .renderingMode(.template)
    .foregroundColor(.white)
    .font(symbolFont)
    .opacity(wasPurchased(item: item))

但据我所知wasPurchased一定是同步函数吧?

But as far as I see wasPurchased must be a synchronous function, right?

类似的东西

func wasPurchased(item: item) -> Bool {
    return check(item:item) ? true : false
}

但是,这样的检查通常是通过网络异步发生的,而且在我看来,该函数必须具有像

But, such checks normally happen asynchronously, over the network, and the function, as I see, must have a signature like

func wasPurchased(item: item, runOnFinishChecking:(Bool)->()) {

该列表是从 Core Data 填充的,就像这样

That list is populated from Core Data, like this

@FetchRequest(fetchRequest: Expressao.getAllItemsRequest())
private var allItems: FetchedResults<Expressao>


var body: some View {
  List {
    ForEach(allItems, id: \.self) { item in
      HStack {
        Text(item.term)
          .font(fontItems)
          .foregroundColor(.white)

        Image(systemName: "lock.circle.fill")
          .renderingMode(.template)
          .foregroundColor(.white)
          .font(symbolFont)
          .opacity(wasPurchased(item: item))
      }
    }
   }
  }

当整个事物是一个数组时,我不知道如何使用异步事物来控制此类元素的不透明度.

I don't see how I can use something asynchronous to control the opacity of such element when the whole thing is an array.

我该怎么做?

推荐答案

只需将您的行内容分离到独立的视图中,并应用上一篇文章中的方法.

Just separate your row content into standalone view and apply approach from previous post.

var body: some View {
  List {
    ForEach(allItems, id: \.self) {
       ExpressaoRowView(item: $0)
    }
  }
}

...

struct ExpressaoRowView: View {
   @ObservedObject var item: Expressao
   
   @State private var locked = true
   
   var body: some View {
      HStack {
        Text(item.term)
          .font(fontItems)
          .foregroundColor(.white)

        Image(systemName: "lock.circle.fill")
          .renderingMode(.template)
          .foregroundColor(.white)
          .font(symbolFont)
          .opacity(self.locked ? 1 : 0)
      }
      .onAppear {
        self.wasPurchased(self.item) { purchased in
          DispatchQueue.main.async {
             self.locked = !purchased
          }
       }
    }
   }
}

注意:您甚至可以将 wasPurchased 检查器放在外面的某个地方(在父级或某个助手中)并作为属性注入到 ExpressaoRowView

Note: you can even keep wasPurchased checker somewhere outside (in parent or in some helper) and inject into ExpressaoRowView as a property

这篇关于SwiftUI - 如何使用列表单元格内的异步操作更改同步值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆