在核心数据中保存一对多关系,但在关闭模拟器后只关系中的一个项目。控制台Swift中的错误

时间:2017-09-24 20:56:15

标签: ios swift core-data swift3 fault

我正在加载一对多的关系(以及关于我的应用初始加载到Core Data中的一堆其他测试数据) Side是一个具有一对多Frames关系的实体:

let sideA = addNewSideToNSManagedObjectContext(orderInFlashcard: 0.0, frames: [frontFrame,backFrame], cardType: newCardType)
        let sideB = addNewSideToNSManagedObjectContext(orderInFlashcard: 1.0, frames: [backFrame,frontFrame], cardType: newCardType)
//context is saved after code above

在加载初始数据之后,我提出了一个tableViewController,我在其中点击一行来显示加载的数据(基本上我显示了一张flashcard&s; side1.frames,然后是flashcard' side2.frames) 当我在初始数据加载后执行此操作并且正常显示时,side1.frames有2个项目,side2.frames有2个项目,它们都显示正确。

问题:当我关闭模拟器并重新运行时(第一次运行后,数据已经存在并且没有再次加载),当我点击显示闪卡时,side1.frames有2个项目但是side2.frames是EMTPY和我得到一个空白显示。我真的很难解决问题所在,因为所有其他数据都是持久存在的。

正在根据当前的闪存卡访问有问题的数据我是这样显示的:

flashcard.cardType.sides
each side.frame (for subviews)

当我呈现边/框时,这里是检索要显示的数据的代码:

func setupFramesForSides(){

        //var framesForSides:[[Frame]] = [[Frame]]() //property //goal to get array order by side.orderInFlashcard of arrays of frames for each side ordered by order in each side
        var sides:[Side] = (flashcard!.cardType!.sides!.allObjects as! [Side])
        sides.sort(by:{$0.orderInFlashcard<$1.orderInFlashcard}) //sides sorted by orderInFlashcard
        currentSideIndex = 0
        print("sides count:\(sides.count)")
        for s in 0..<sides.count{
            print("side:\(sides[s].orderInFlashcard)")
        }
        for sideIndex in 0..<sides.count{
            let side:Side = sides[sideIndex]
            print("currentSideIndex: \(sideIndex)")
            print("currentSide: \(side)")
            //create sorted array of frames for the side
            let frames:[Frame] = side.frames!.allObjects as! [Frame]
            print("framesAvailable:\(frames)")
            print("framesAvailable.count:\(frames.count)")
            //frames.sort(by:{$0<$1})
            var frameOrdersInSide:[Int:Frame] = [Int:Frame]()
            print("PREPARING NEW SIDE'S FRAMES")
            for sideFrame in frames{
                for orderInSide in sideFrame.orderInSides!.allObjects as! [FrameOrderInSide]{
                    if orderInSide.side == side {
                        print("FOUND MATCHING SIDE IN FRAME.ORDERINSIDES")
                        print("orderInSide:\(orderInSide.index)")
                        print("sideFrame:\(sideFrame)")
                        frameOrdersInSide[Int(orderInSide.index)] = sideFrame //index for frame in this side should never match another frame's index
                    }
                }
            }
            print("frameOrdersInSide:\(frameOrdersInSide)")
            print("frameOrdersInSide.count:\(frameOrdersInSide.count)")
            var sortedFramesInSide:[Frame] = [Frame]()
            for frameIndexInSide in 0..<(frameOrdersInSide.keys.sorted(by:{$0<$1})).count{
                sortedFramesInSide.append(frameOrdersInSide[frameIndexInSide]!) //array of Frames in order of the index in the side
            }
            framesForSides.append(sortedFramesInSide)
        }

以下是在不关闭模拟器的情况下从数据的初始加载显示的控制台结果:

sides count:2
side:0.0
side:1.0
currentSideIndex: 0
currentSide: <FlashTags.Side: 0x6000000898d0> (entity: Side; id: 0xd000000000040010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Side/p1> ; data: {
    frames =     (
        "0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1>",
        "0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2>"
    );
    orderInFlashcard = 0;
})
framesAvailable:[<FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
}), <FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
})]
framesAvailable.count:2
PREPARING NEW SIDE'S FRAMES
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:1.0
sideFrame:<FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
})
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:0.0
sideFrame:<FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
})
frameOrdersInSide:[0: <FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
}), 1: <FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
})]
frameOrdersInSide.count:2
currentSideIndex: 1
currentSide: <FlashTags.Side: 0x61800008a7d0> (entity: Side; id: 0xd000000000080010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Side/p2> ; data: {
    frames =     (
        "0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1>",
        "0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2>"
    );
    orderInFlashcard = 1;
})
framesAvailable:[<FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
}), <FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
})]
framesAvailable.count:2
PREPARING NEW SIDE'S FRAMES
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:0.0
sideFrame:<FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
})
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:1.0
sideFrame:<FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
})
frameOrdersInSide:[0: <FlashTags.Frame: 0x600000089740> (entity: Frame; id: 0xd00000000004000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd00000000008000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd00000000004000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
}), 1: <FlashTags.Frame: 0x600000089510> (entity: Frame; id: 0xd00000000008000a <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd00000000010000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>",
        "0xd0000000000c000c <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>"
    );
})]
frameOrdersInSide.count:2

关闭模拟器后,这就是控制台中出现的问题:

sides count:2
side:0.0
side:1.0
currentSideIndex: 0
currentSide: <FlashTags.Side: 0x600000289420> (entity: Side; id: 0xd00000000004000e <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Side/p1> ; data: {
    frames = "<relationship fault: 0x600000028940 'frames'>";
    orderInFlashcard = 0;
})
framesAvailable:[<FlashTags.Frame: 0x600000286810> (entity: Frame; id: 0xd000000000040010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: <fault>), <FlashTags.Frame: 0x600000286bd0> (entity: Frame; id: 0xd000000000080010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: <fault>)]
framesAvailable.count:2
PREPARING NEW SIDE'S FRAMES
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:1.0
sideFrame:<FlashTags.Frame: 0x600000286810> (entity: Frame; id: 0xd000000000040010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd000000000080012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd000000000040012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
})
FOUND MATCHING SIDE IN FRAME.ORDERINSIDES
orderInSide:0.0
sideFrame:<FlashTags.Frame: 0x600000286bd0> (entity: Frame; id: 0xd000000000080010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd0000000000c0012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>",
        "0xd000000000100012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>"
    );
})
frameOrdersInSide:[0: <FlashTags.Frame: 0x600000286bd0> (entity: Frame; id: 0xd000000000080010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p2> ; data: {
    html = "{{englishKeyword}}";
    name = front;
    orderInSides =     (
        "0xd0000000000c0012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p3>",
        "0xd000000000100012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p4>"
    );
}), 1: <FlashTags.Frame: 0x600000286810> (entity: Frame; id: 0xd000000000040010 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Frame/p1> ; data: {
    html = "{{kanjiImage}}";
    name = back;
    orderInSides =     (
        "0xd000000000080012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p2>",
        "0xd000000000040012 <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/FrameOrderInSide/p1>"
    );
})]
frameOrdersInSide.count:2
currentSideIndex: 1
currentSide: <FlashTags.Side: 0x6000002894c0> (entity: Side; id: 0xd00000000008000e <x-coredata://AABA51C1-C3ED-4701-A7DD-BE1CB7B66995/Side/p2> ; data: {
    frames = "<relationship fault: 0x600000029b00 'frames'>";
    orderInFlashcard = 1;
})
framesAvailable:[]
framesAvailable.count:0
PREPARING NEW SIDE'S FRAMES
frameOrdersInSide:[:]
frameOrdersInSide.count:0

1 个答案:

答案 0 :(得分:0)

更改核心数据设计后,更新了闪卡加载代码:

var sides:[Side] = (flashcard!.cardType!.sides!.allObjects as! [Side])
        sides.sort(by:{$0.orderInFlashcard<$1.orderInFlashcard}) //sides sorted by orderInFlashcard
        currentSideIndex = 0

        for sideIndex in 0..<sides.count{
            let side:Side = sides[sideIndex]
            let framesOrderInSide:[FrameOrderInSide] = (side.frameOrders!.allObjects as! [FrameOrderInSide]).sorted(by:{$0.index<$1.index})
            framesForSides.append(framesOrderInSide.map({$0.frame}))
        }