Home > front end >  RealmSwift - Passing query as dynamic parameter fails
RealmSwift - Passing query as dynamic parameter fails

Time:12-30

I'm having an issue passing a query as a function parameter. Although the type of the query is the same as when used directly within the class, (and when I copy & paste the code, it works), it won't work – returns a basic strange error:

This is where I'm accessing the filter:

Cannot convert value of type 'Bool' to closure result type 'Query<Bool>'.

import SwiftUI
import RealmSwift

struct PostFeed: View {
    @ObservedResults(Post.self) private var posts
    
    init(where filter: ((Query<Post>) -> Query<Bool>)?) {
        self._posts = .init(Post.self, where: filter )
    }
}

And this is where I am passing the filter:

PostFeed { $0.posterId == id } // <-- Error appears here

...

If I remove the dynamic query parameter and enter the same query inside my view, it works. The query is otherwise valid if I structure it as:

init(_ id: String) {
    self.id = id
    self._posts = .init(Post.self, where: {$0.posterId == id} )
}

and

PostFeed(id)

I would like to make my example 1 work. Any recommendations?

CodePudding user response:

What about changing the parameter type to

init(where filter: ((Query<Post>) -> Bool)?) {
    self._posts = .init(Post.self, where: filter)
}

CodePudding user response:

After asking a colleague to check the issue, we discovered the code was working as normal.

Command K (Clean build folder) fixed the issue. Classic XCode move.

  • Related