Swift 触摸过程中核心数据索引的变化

Swift 触摸过程中核心数据索引的变化,swift,core-data,indexing,sprite-kit,Swift,Core Data,Indexing,Sprite Kit,我有一个使用核心数据的SpriteKit应用程序。主游戏场景中有许多精灵(15个左右)。其中,有3个具有特殊特征(例如名称、颜色、类型和描述)的精灵。精灵从viewDidLoad()中的核心数据存储中拾取 这只是想让大家了解一下正在发生的事情,但我还没有找到一种模式,除了调用touchesbearth()时问题似乎就开始了。touchesStart()下有多条记录的原因是,在从touchesCancelled或toucheseEnded()返回结果之前,touchesStart()上返回的结果介

我有一个使用核心数据的SpriteKit应用程序。主游戏场景中有许多精灵(15个左右)。其中,有3个具有特殊特征(例如名称、颜色、类型和描述)的精灵。精灵从viewDidLoad()中的核心数据存储中拾取

这只是想让大家了解一下正在发生的事情,但我还没有找到一种模式,除了调用touchesbearth()时问题似乎就开始了。touchesStart()下有多条记录的原因是,在从touchesCancelled或toucheseEnded()返回结果之前,touchesStart()上返回的结果介于1和4之间。只有这一个属性在移动

一旦运行了didMoveToView,记录就应该在存储区中。然后,此问题有时会影响与所接触节点对应的记录,有时只影响其他记录,有时影响所有三条记录。虽然我在touchsbegind()中看到了重复的记录,但在touchscancelled()或touchsendd()中,我从来没有看到任何重复的记录,只是记录移动到了错误的位置。我已经在我的应用程序的其他地方寻找过类似的问题,但其余的都是稳定的。我已经在模拟器和iPhone上重现了这个问题。我还重置了模拟器,并在应用程序上执行了清理和构建。还是一样的问题

ProjectProblem类如下所示:

import UIKit
import CoreData

class ProjectProblem {
    func projectDescriptionProblem () {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = appDelegate.managedObjectContext
        let request = NSFetchRequest(entityName: projectEntityName)
        do {
            let objectList = try context.executeFetchRequest(request)
            for thisObject in objectList {
                print("DESCRIPTION: \(thisObject.valueForKey(descriptionKey) as! String)")
            }
        }
        catch _ as NSError {
           print("descriptionError")
        }
    }
}
有人知道我能做些什么来解决这个问题吗

更新: 我已经运行了许多模拟,并改变了在其他场景中执行任何
setValue
写入“description”字段的方式。我发现了两件事。 1) 更改代码中其他地方的写入方式会带来改进。现在只有数据存储中的最后一条记录在更改,而没有调用
setValue
。执行写入操作的场景是关闭的,游戏场景在加载后处于稳定状态,但在某些地方,核心数据写入操作挂在后台。当触摸在游戏场景中被调用时,这些被触发。根据经验,越早用代码写入核心数据越好(即,在远离该场景的过渡之前)。
2) 尽管情况有所改善,但仍然存在一个问题;数据存储中的最后一条记录仍在更改,而未调用
setValue
。发生的情况最好描述如下:

e.g. 5 nodes, numbered 1 to 5; each node corresponds to a different core data entry ('A', 'B', 'C', 'D', 'E', respectively).
- touch node_1 and nothing happens to the records in the data store; they stay the same which is what they should do as I don't do any new writes to the data store
- touch node_2 and 'E' is replaced in the final record (the one corresponding to node 5) with 'A'; the first 4 entries stay the same (so it looks like ('A', 'B', 'C', 'D', 'A') now)
- touch node_3 and 'A' is replaced in the final record with 'B'; again the others stay the same ('A', 'B', 'C', 'D', 'B')
- touch node_4 and 'B' is replaced in the final record with 'C' ('A', 'B', 'C', 'D', 'C')
- touch node_5, which corresponds with entry 5 and 'C' becomes 'D' ('A', 'B', 'C', 'D', 'D')
- touch any other node again and the entry in the final record stays the same ('A', 'B', 'C', 'D', 'D')
基本上,该记录正在更改为与数据存储中的另一条记录相同的条目(包括它自己的条目)。它不是对应于被触摸节点的条目,而是对应于它之前被触摸节点的条目


我怀疑在写入数据存储的方式中存在一些延迟,代码的其他部分可能会超过这些延迟(例如从场景转移)。这可能会在某个地方留下一个“写线程”。循环
do之前,设定值保持打开状态{…let objectList…for this Object in…
已完成。下次在应用程序中执行操作时,将从核心数据堆栈中调用这些。如果是这种情况,是否有人有类似的经验,如何找到这些,或者更好的是,从代码中清除它们?

是否可以发布
项目问题的代码
?尝试添加排序描述符:
request.sortDescriptors=[NSSortDescriptor(key:“descriptionKey”,升序:true)]
这没有帮助,因为排序描述符只是将数据按正确的顺序排列,隐藏了记录被交换的事实。我已经做了一些故障排除,并将问题简化为一个问题。上面有解释。我正在努力按照您对问题的描述进行操作。您能显示
项目的实际输出吗描述问题
调用?并且
didMoveToView
touch…
方法中的任何内容似乎都不会以“node is…”的形式生成输出。我在其他地方发现了一个问题。我正在使用Firebase在另一个场景中的代码的另一部分中将数据存储在云中。在两个存储之间使用字典时,不会与核心数据直接交互。我可以打开和关闭问题(在核心数据中重新索引一个属性的数据)禁用Firebase。这很复杂,从助手类生成输出并不能让它更清晰(上面所有的数据,如“node is…”实际上与助手的输出相同)。
import UIKit
import CoreData

class ProjectProblem {
    func projectDescriptionProblem () {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = appDelegate.managedObjectContext
        let request = NSFetchRequest(entityName: projectEntityName)
        do {
            let objectList = try context.executeFetchRequest(request)
            for thisObject in objectList {
                print("DESCRIPTION: \(thisObject.valueForKey(descriptionKey) as! String)")
            }
        }
        catch _ as NSError {
           print("descriptionError")
        }
    }
}
e.g. 5 nodes, numbered 1 to 5; each node corresponds to a different core data entry ('A', 'B', 'C', 'D', 'E', respectively).
- touch node_1 and nothing happens to the records in the data store; they stay the same which is what they should do as I don't do any new writes to the data store
- touch node_2 and 'E' is replaced in the final record (the one corresponding to node 5) with 'A'; the first 4 entries stay the same (so it looks like ('A', 'B', 'C', 'D', 'A') now)
- touch node_3 and 'A' is replaced in the final record with 'B'; again the others stay the same ('A', 'B', 'C', 'D', 'B')
- touch node_4 and 'B' is replaced in the final record with 'C' ('A', 'B', 'C', 'D', 'C')
- touch node_5, which corresponds with entry 5 and 'C' becomes 'D' ('A', 'B', 'C', 'D', 'D')
- touch any other node again and the entry in the final record stays the same ('A', 'B', 'C', 'D', 'D')