CoreStore refetch predicate NSFetchRequest issue

邮差的信 提交于 2020-01-25 09:34:07

问题


if I got it clear I need to refetch monitor when I search something:

I have this function to refetch with provided string

func search(searchText: String) {
        self.monitor.refetch(.where(format: "%K CONTAINS[cd] %@", #keyPath(ListEntityType.name), searchText),
                             OrderBy<ListEntityType>(.ascending("name")))
    }

but this code is not compilable, only this below:

func search(searchText: String) {
        self.monitor.refetch(Where<ListEntityType>("name", isEqualTo: searchText),
                             OrderBy<ListEntityType>(.ascending("name")))
    }

But here is only equal operation, but I need to search not just entire word but as well substring with format: "%K CONTAINS[cd] %@

From CoreStore this is how function refetch looks like:

 public func refetch(_ fetchClauses: [FetchClause]) {

        self.refetch { (fetchRequest) in

            fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
        }
    }

public protocol FetchClause {

    func applyToFetchRequest<T>(_ fetchRequest: NSFetchRequest<T>)
}

回答1:


Just replace this:

.where(format: "%K CONTAINS[cd] %@", #keyPath(ListEntityType.name), searchText)

with

Where<ListEntityType>("%K CONTAINS[cd] %@", #keyPath(ListEntityType.name), searchText)

Another syntax sugar: You can get rid of the #keyPath references and use \ListEntityType.name instead



来源:https://stackoverflow.com/questions/57511579/corestore-refetch-predicate-nsfetchrequest-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!