SwiftUI-如何在init中使用fetchRequest更新数据 [英] SwiftUI - how to update data with fetchRequest in init

查看:40
本文介绍了SwiftUI-如何在init中使用fetchRequest更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在View的 init()方法中进行CoreData提取.我没有在SwiftUI中使用FetchRequest,因为我需要一个基于参数的谓词,该谓词也将发送到View.在@FetchRequest中使用该参数会导致错误,因为该变量尚未初始化.

I am doing a CoreData fetch inside my init() method of the View. I am not using FetchRequest in SwiftUI, as I need a predicate based on a parameter which is send to the View aswell. Using that parameter in the @FetchRequest will cause an error, as the variable has not been initialized.

我正在执行init()内部的提取请求

I am doing following fetch Request inside the init()

//FetchRequest
let fetch : NSFetchRequest<Article>
self.articleRows = [Article]()
fetch = Article.fetchRequest() as! NSFetchRequest<Article>
fetch.predicate = NSPredicate(format: "type == %d", self.type)

do {
    self.articleRows = try NSManagedObjectContext.current.fetch(fetch)
} catch
{
}

那很好.我将所有数据显示在 ForEach 循环中.

That is working fine. I am displaying all my data inside a ForEach loop.

ForEach(self.articleRows, id:\.self) { article in

但是,当我从上下文中删除实体时,我需要刷新视图以显示更改.在执行删除操作时,我切换一个 @State 变量以刷新视图.那行得通,但是实体仍然在我的数组内.那是由于没有再次调用 init()且不再提出获取请求的事实.如果将调用 init(),则删除的实体将不再存在.

However, when I am deleting an entity from my context I need to refresh the view to display the changes. On delete action I toggle a @State variable, to refresh the view. That works, however the entity is still inside my Array. Thats due to the fact that init() is not called again and the fetch request is not made again. If init() would be called, the deleted entity wouldn't be there anymore.

这里最好的方法是什么?如何重新获取我的CoreData实体.

What is the best approach here? How can I refetch my CoreData entities.

我当前的解决方案:我目前在抓取"视图中使用 Binding .如果进行更改,则切换该绑定,然后重新加载父视图.这将导致我的子视图(fetch View)也重新加载,并且再次提出了获取请求.那是最好的方法吗?有更简单的解决方案吗?

My current solution: I am currently using a Binding inside my Fetch view. If I make changes I toggle that binding and my parent view reloads. The will cause my child view (fetch View) to reload aswell and the fetch request is made again. Is that the best way? Any simpler solution for that?

推荐答案

因此,我找到了解决问题的方法.

So, I found a way of solving my problem.

出了什么问题?

由于我未使用FetchRequest属性包装器,因此视图未更新,因为在该FetchRequest中需要一个实例变量.所以我需要在我的 Init()方法中进行Fetching.但是我所做的是,我只在 init()中提取了一次项目,除非重新加载了父项,否则不会更新该项目.

The View was not updating because I wasn't using FetchRequest property wrapper, because I need a instance variable in that FetchRequest. So I need to do the Fetching inside my Init() method. But what I did was, I just fetched my items once in the init() and that won't be updated unless the parent with is reloaded.

如何解决而又不烦父母的更新?

我使用了FetchRequest并在init()中对其进行了初始化,而不是仅在 init()中进行一次手动提取,因此它仍像FetchRequest属性包装一样,就像可观察对象一样.

Instead of doing a manual fetch only once in the init() I used a FetchRequest and initialized it in the init(), so it still behaves as FetchRequest property wrapper, like an Observable Object.

我这样宣布:

@FetchRequest var articleRows : FetchedResults<Article>

init()

//Here in my predicate I use self.type variable 
var predicate = NSPredicate(format: "type == %d", self.type)

//Intialize the FetchRequest property wrapper
self._articleRows = FetchRequest(entity: Article.entity(), sortDescriptors: [], predicate: predicate)

这篇关于SwiftUI-如何在init中使用fetchRequest更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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