Swift-核心添加的意外行

发布于 2025-01-23 13:36:31 字数 1238 浏览 1 评论 0原文

我有一个带有6行的Coredata底座。

i na viewController,数据显示在表中,当我在表中选择一行时,didSelectrow 列出6行。都是行。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    caches = CoreData.getCaches()
    print ("Amount \(caches.count)") // gives 6

    performSegue(withIdentifier: "Select", sender: nil)
}

执行SEGUE时,将执行PreparForsegue。现在,具有相同的命令与值7。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    caches = CoreData.getCaches()
    print ("Amount \(caches.count)") // gives 7
}

我怀疑背景中的某些事情正在发生,但是我找不到什么。 以下是参考的静态方法:

static func getCaches() -> [Caches] {

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var resultArray: [Caches] = []

    let request = NSFetchRequest<Caches>(entityName: "Caches")
    request.returnsObjectsAsFaults = false

    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    let sortDescriptors = [sortDescriptor]
    request.sortDescriptors = sortDescriptors

    do {
        resultArray = try context.fetch(request)
    } catch {
        print("Error - \(error)")
    }
    return resultArray
}

I have a CoreData base with 6 rows in it.

I na ViewController, the data is displayed in a UITable, when I select a row in the table, the didSelectRow
lists 6 rows. That are all the rows.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    caches = CoreData.getCaches()
    print ("Amount \(caches.count)") // gives 6

    performSegue(withIdentifier: "Select", sender: nil)
}

When the Segue is executed the prepareForSegue is executed. Now the same command results with the value 7.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    caches = CoreData.getCaches()
    print ("Amount \(caches.count)") // gives 7
}

I suspect that something in the background is happening, but i can't find out what.
Below is the static method for reference:

static func getCaches() -> [Caches] {

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var resultArray: [Caches] = []

    let request = NSFetchRequest<Caches>(entityName: "Caches")
    request.returnsObjectsAsFaults = false

    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    let sortDescriptors = [sortDescriptor]
    request.sortDescriptors = sortDescriptors

    do {
        resultArray = try context.fetch(request)
    } catch {
        print("Error - \(error)")
    }
    return resultArray
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-01-30 13:36:31

经过大量搜索,我找到了它。

我执行了一个performegewithIdentifier。在调用ViewController中调用PreparForsegue。但是显然,在此之前,创建了来自调用VC的变量/属性。 (如果您想到一些想法,这是合乎逻辑的)

在所谓的VC中,使用以下代码初始化了一个变量
(从网上的某个地方复制)

var cache = Caches((context: (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext))

这条颂歌引起了麻烦。因为它在持久性范围内创建一个实体(未写入实际的核心)。我用一个普通的旧代替了它:

var cache = Caches()

现在一切正常。感谢您的支持。

After a lot of searching I found it.

I execute a performSegueWithIdentifier. Which calls the prepareForSegue in the calling ViewController. But apparently before that, the variables/properties from the called VC are created. (Which is logical if you give it some thought)

In the called VC, a variable was initialised with the following code
(copied from somewhere on the net)

var cache = Caches((context: (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext))

This line of ode was causing the trouble. Cause it creates an entity in the persistentContainer (not written to the actual CoreData). I replaced it with a plain old:

var cache = Caches()

And everything is working okay now. Thanks for the support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文