如何在一个视图中发出多个 Fe​​tch 请求(SwiftUI)

Posted

技术标签:

【中文标题】如何在一个视图中发出多个 Fe​​tch 请求(SwiftUI)【英文标题】:How to make multiple FetchRequests in one view (SwiftUI) 【发布时间】:2021-11-04 11:43:30 【问题描述】:

我正在尝试处理一个学校项目,但我无法理解如何获取多个特定数据集。

我有一个名为 GameView 的视图,其中有以下 FetchRequest:

FetchRequest(entity: Player.entity(), sortDescriptors: [], predicate: NSPredicate(format: "isPlaying == %@", true))
var allPlayers: FetchedResults<Player>

“玩家”是一个核心数据实体,具有firstNamelastNamePositionisPlaying 属性

但是,我需要能够根据位置属性为特定玩家分配特定角色。例如,我想将每个具有位置属性“P”的 Player 实体分配给“Pitchers”的 @State 数组,并且我想将每个具有位置属性“SS”的 Player 实体分配给 @State 数组'游击手'。

我唯一想到的就是使用两个不同的谓词发出两个单独的提取请求,但我记得每个视图只能发出一个提取请求。我该如何解决这个问题?

这是我的一些代码。以前,我使用 Observable Object 分配位置,但我想换掉它。

struct GameView: View 
    @ObservedObject var TeamNamesAndColors: TeamAttributes
    // declares all the different variables necessary to represent game state
    @State var GameIsOver: Bool = false
    @State var ProceedToResults: Bool = false
    @State var IsInningTop: Bool = true
    @State var InningNumber: Int = 1
    @State var CurrentBatter: [String: String] = ["FirstName": "", "LastName": "", "Position": ""]
    @State var HitterIndexHome: Int = 1
    @State var HitterIndexAway: Int = 1
    @State var AwayPitcher: [String: String] = ["FirstName": "", "LastName": ""]
    @State var HomePitcher: [String: String] = ["FirstName": "", "LastName": ""]
    @State var CurrentPitcher: [String: String] = ["FirstName": "", "LastName": "", "Position": ""]
    @State var BSOCount: [String: Int] = ["Balls": 0, "Strikes": 0, "Outs": 0]
    @State var BasesState: [String: Bool] = ["1B": false, "2B": false, "3B": false]
    @State var AwayTeamScore: Int = 0
    @State var HomeTeamScore: Int = 0
    // used to present different navigation buttons or Pitch Sheet
    @State var showPitchView = false
    @State var goBackHome = false
    @State var finishEarly = false
    
    // checks which position in the lineup is the pitcher, and assigns it the pitcher role
    func SetAwayPitcher() 
        if AnHDetails.Hitter1APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter1AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter1ALast
         else if AnHDetails.Hitter2APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter2AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter2ALast
         else if AnHDetails.Hitter3APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter3AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter3ALast
         else if AnHDetails.Hitter4APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter4AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter4ALast
         else if AnHDetails.Hitter5APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter5AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter5ALast
         else if AnHDetails.Hitter6APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter6AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter6ALast
         else if AnHDetails.Hitter7APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter7AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter7ALast
         else if AnHDetails.Hitter8APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter8AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter8ALast
         else if AnHDetails.Hitter9APos == "P" 
            AwayPitcher["FirstName"] = AnHDetails.hitter9AFirst
            AwayPitcher["LastName"] = AnHDetails.hitter9ALast
        
    
    // same thing as previous func
    func SetHomePitcher () 
        if AnHDetails.Hitter1HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter1HFirst
            HomePitcher["LastName"] = AnHDetails.hitter1HLast
         else if AnHDetails.Hitter2HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter2HFirst
            HomePitcher["LastName"] = AnHDetails.hitter2HLast
         else if AnHDetails.Hitter3HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter3HFirst
            HomePitcher["LastName"] = AnHDetails.hitter3HLast
         else if AnHDetails.Hitter4HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter4HFirst
            HomePitcher["LastName"] = AnHDetails.hitter4HLast
         else if AnHDetails.Hitter5HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter5HFirst
            HomePitcher["LastName"] = AnHDetails.hitter5HLast
         else if AnHDetails.Hitter6HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter6HFirst
            HomePitcher["LastName"] = AnHDetails.hitter6HLast
         else if AnHDetails.Hitter7HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter7HFirst
            HomePitcher["LastName"] = AnHDetails.hitter7HLast
         else if AnHDetails.Hitter8HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter8HFirst
            HomePitcher["LastName"] = AnHDetails.hitter8HLast
         else if AnHDetails.Hitter9HPos == "P" 
            HomePitcher["FirstName"] = AnHDetails.hitter9HFirst
            HomePitcher["LastName"] = AnHDetails.hitter9HLast
        
    
    // decides batter's position and initializes CurrentBatter
    func SetFirstHitter() 
        CurrentBatter["FirstName"] = AnHDetails.hitter1AFirst
        CurrentBatter["LastName"] = AnHDetails.hitter1ALast
        CurrentBatter["Position"] = AnHDetails.Hitter1APos
    
    // sets the HomePitcher to be the active pitcher to begin the game
    func SetFirstPitcher() 
        CurrentPitcher["FirstName"] = HomePitcher["FirstName"]
        CurrentPitcher["LastName"] = HomePitcher["LastName"]
    

【问题讨论】:

我们需要一些代码来解决这个问题,但通常情况下,您无需将 CoreData 属性放入单独的数组中即可随意使用。 哦,好吧。否则我将如何识别它们?获取结果后,如何访问某些特定的结果? .filter 是一种方式。 我很困惑。假设我有以下内容: func SetFirstHitter() CurrentBatter["FirstName"] = CurrentBatter["LastName"] = CurrentBatter["Position"] = 我会这样做吗? 我最初避免这样做是因为您使用的是 CoreData,但请参阅:How do I ask a good question? 和 How to create a Minimal, Reproducible Example。您发布的代码的问题只是它是一组用于过滤某些玩家的蛮力函数。但是,回答您的问题所需的代码是 var body 代码,它显示了您希望如何使用它。您是在修改属性还是仅显示它们?我们无法以这种方式帮助您。 【参考方案1】:

这是一种使用两个 ForEach 的方法作为列表内的部分(ios 15):

struct TwoFetchedRequests: View 
    // #1
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.sort, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>

    // #2
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.sort, ascending: true)],
        animation: .default)
    private var favorites: FetchedResults<Favorite>

    var body: some View 
       List 
           // #1
           Section 
               ForEach(items)  item in
                   Text(item.fullName)
               
            header: 
               Text("Items")
           
           
           // #2
           Section 
               ForEach(favorites)  fav in
                   Text(fav.fullName)
               
            header: 
               Text("Favorites")
           
        
    

【讨论】:

以上是关于如何在一个视图中发出多个 Fe​​tch 请求(SwiftUI)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用操作查询发出多个 Alamofire 请求

如何在网络视图中查看 NTLM 挑战?

如何在 Express 和 Mongoose 的一个控制器 API 内发出多个“查找”请求?

当您不知道页数时,如何使用 Node.js 在 while 循环中向 API 发出多个分页 GET 请求?

如何从Google Cloud Function(Cheerio,Node.js)发出多个http请求

使用过期令牌发出同时 API 请求时如何避免多个令牌刷新请求