数据被覆盖:核心数据

时间:2017-05-22 10:39:49

标签: ios swift core-data swift3

我正在尝试在核心数据中保存当前日期的随机数。但这个数字正在越来越多。这是我的代码:

func saveRandNumbers (number : Int){

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }

        let date = Date();

        let managedContext = appDelegate.persistentContainer.viewContext;

        let entity = NSEntityDescription.entity(forEntityName: "RandNumbers", in: managedContext);

        let randNumbers = NSManagedObject(entity: entity!, insertInto : managedContext);

        randNumbers.setValue(number , forKeyPath : "randNumber");
        randNumbers.setValue(date , forKeyPath : "time");

        do{
            try managedContext.save()
        }catch let error as NSError{
            print("Could not save\(error)");
        }
    }

这是我为演示测试它的方式。在viewDidLoad()

我用两个diff值调用该函数:

saveRandNumbers (number : 3)
saveRandNumbers (number : 4)

获取数据的代码:

func fetchRandNumbers (){

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }

        let managedContect = appDelegate.persistentContainer.viewContext;

        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName : "RandNumbers")

        do{
            RandNumbers = try managedContect.fetch(fetchRequest)
            print("Result :\(RandNumbers)")
        }catch let error as NSError{
            print("Could not fetch \(error)");
        }

    }

输出:

[<RandNumbers: 0x608000099690> (entity: RandNumbers; id: 0xd000000000040000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p1> ; data: <fault>), <RandNumbers: 0x60800009a400> (entity: RandNumbers; id: 0xd000000000080000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p2> ; data: <fault>), <RandNumbers: 0x60800009ac70> (entity: RandNumbers; id: 0xd0000000000c0000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p3> ; data: <fault>), <RandNumbers: 0x60800009acc0> (entity: RandNumbers; id: 0xd000000000100000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p4> ; data: <fault>), <RandNumbers: 0x60800009adb0> (entity: RandNumbers; id: 0xd000000000140000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p5> ; data: <fault>), <RandNumbers: 0x60000009d470> (entity: RandNumbers; id: 0xd000000000180000 <x-coredata://B35EDED6-9C24-45E7-8F05-D2CB5F5FE4BA/RandNumbers/p6> ; data: {
    randNumber = 4;
    time = nil;
})]

当我打印结果时,我只得到第二个值。任何帮助,将不胜感激。感谢。

2 个答案:

答案 0 :(得分:1)

你已经获得了一系列结果;您需要遍历此数组才能访问每个项目。打印数组时,对象是错误的;占位符对象,当您访问它时将替换为实际对象。

func fetchRandNumbers(){

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }

    let managedContect = appDelegate.persistentContainer.viewContext;

    let fetchRequest: NSFetchRequest<RandNumbers> = RandNumbers.fetchRequest()

    do{
        let randNumbers = try managedContect.fetch(fetchRequest)
        for randNumber in randNumbers {
            print("random number \(randNumber.number) was saved at \(randNumber.date)")
        }
    }catch let error as NSError{
        print("Could not fetch \(error)");
    }

}

答案 1 :(得分:0)

循环fetchedResults并打印每个randNumber

RandNumbers = try managedContect.fetch(fetchRequest)
for randNumber in RandNumbers {
    print(randNumber)
}