将最后一个数据插入核心数据库



我开始用Swift编程,我的项目需要DB。我使用Core Data,有很多问题。

最重要的问题之一是"插入到数据库",插入后我取数据,它返回的数据列表,只有最后一个对象是OK的,所有的最后一个对象是下面是我的insert, fetch和获取数据的日志:

        let newItem = NSEntityDescription.insertNewObjectForEntityForName("DeviceList", inManagedObjectContext:managedContext) as! DeviceList
    newItem.mac = deviceObject.mac
    newItem.name = deviceObject.name
    //4
    var error: NSError?
    if !managedContext.save(&error) {
        println("Could not save (error), (error?.userInfo)")
        return false
    }

获取

let fetchRequest = NSFetchRequest(entityName: "DeviceList")
 if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [String] {
        result = fetchResults
}
print("result:(result)")
日志

"<MyAPP.DeviceList: 0x7be58fe0> (entity: DeviceList; id: 0x7be6f480 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p1> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7be600f0> (entity: DeviceList; id: 0x7be59010 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p2> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7bea5e30> (entity: DeviceList; id: 0x7be60120 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p3> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7be6e0e0> (entity: DeviceList; id: 0x7bea5e60 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p4> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaaa00> (entity: DeviceList; id: 0x7beac3c0 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p5> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaaae0> (entity: DeviceList; id: 0x7be6e150 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p6> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaac00> (entity: DeviceList; id: 0x7beaab50 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p7> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7bea3600> (entity: DeviceList; id: 0x7beaac30 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p8> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7c03f5f0> (entity: DeviceList; id: 0x7c03c2a0 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p9> ; data: {n    mac = "00:00:00:13:43:59";n    name = Atim;n})"

我的插入函数有问题吗?

我还打开了sqlite数据库,存储在模拟器的SQLiteManager (FireFox ad-one),它不显示在DB中的任何记录,为什么?当我用Objective-C编程时,sqlite文件显示所有数据没有任何问题!

尝试从模拟器中删除应用程序并重新开始,因为旧的记录被保留,除非您删除应用程序或使用删除操作。您还可以更改您的模拟器deviceType或版本为test。

最新更新