how to init multi dynamic fetch requests in one view?

45 Views Asked by At

One view needs multi dynamic fetchresults, so I write down the following code:

 @FetchRequest var dreamsForTheUser: FetchedResults<Dream>
    
    @FetchRequest var actionsForTheUser: FetchedResults<Action>
    
    @FetchRequest var actions: FetchedResults<Action>

    init(filterForTheUser: String, filter: String) {
        _dreamsForTheUser = FetchRequest<Dream>(sortDescriptors: [],predicate: NSPredicate(format: "dreamID BEGINSWITH %@", filterForTheUser))
        
        _actionsForTheUser = FetchRequest<Action>(sortDescriptors: [],predicate: NSPredicate(format: "actionID BEGINSWITH %@", filterForTheUser))
        
        _actions = FetchRequest<Action>(sortDescriptors: [],predicate: NSPredicate(format: "actionID == %@", filter))
    }

And I let the view show in another view with the following code:

 .sheet(isPresented: $editCurrentAction,content: {
            EditActionView(filterForTheUser: (userViewModel.userID ?? ""), filter: (action.actionID ?? ""))
        })
        

But the fetch results are wrong. It seems that the three fetchrequests use the same filter. I think there are something wrong in the code about .init. Could you help to check it? Really thanks.

Could you help to check the codes and point out the error?

0

There are 0 best solutions below