Swift-核心添加的意外行
我有一个带有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量搜索,我找到了它。
我执行了一个performegewithIdentifier。在调用ViewController中调用PreparForsegue。但是显然,在此之前,创建了来自调用VC的变量/属性。 (如果您想到一些想法,这是合乎逻辑的)
在所谓的VC中,使用以下代码初始化了一个变量
(从网上的某个地方复制)
这条颂歌引起了麻烦。因为它在持久性范围内创建一个实体(未写入实际的核心)。我用一个普通的旧代替了它:
现在一切正常。感谢您的支持。
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)
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:
And everything is working okay now. Thanks for the support.