CoreStore 重新获取谓词 NSFetchRequest 问题 [英] CoreStore refetch predicate NSFetchRequest issue

查看:57
本文介绍了CoreStore 重新获取谓词 NSFetchRequest 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我清楚我需要在搜索内容时重新获取监视器:

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")))
    }

但这里只是相同的操作,但我不仅需要搜索整个单词,还需要搜索具有 格式的子字符串:%K CONTAINS[cd] %@

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

来自 CoreStore 的函数 refetch 是这样的:

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>)
}

推荐答案

只需替换:

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

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

另一种语法糖:您可以去掉#keyPath 引用并使用\ListEntityType.name 代替

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

这篇关于CoreStore 重新获取谓词 NSFetchRequest 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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