2015-09-14 124 views
0

我開始用Swift編程,我的項目需要DB。我使用核心數據,並有很多問題。插入核心數據courpet我的最後一個數據

其中一個最重要的問題是「插入數據庫」,插入我獲取數據後,它返回的數據列表只有最後一個對象是OK,所有最後的對象都是 這裏是我的插入,獲取和日誌獲取的數據:

插入

 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})" 

是它在我的插入功能的任何問題?

而且我還通過SQLiteManager(FireFox ad-one)打開了存儲在模擬器中的sqlite數據庫,它沒有在數據庫中顯示任何記錄,爲什麼?當我用Objective-C編程時,sqlite文件顯示所有數據都沒有問題!

+0

有什麼讓我吃驚的是!當我通過「名稱」獲取「mac設備」時,數據按預期返回!實際上,我認爲我無法記錄所有設備,並且無法在調試器中看到數據,即使我的記錄已返回! –

回答

0

嘗試從模擬器中刪除應用程序並重新開始,因爲除非刪除應用程序或使用刪除操作,否則保留舊記錄。您還可以更改您的模擬器deviceType或版本以進行測試。

+0

我做到了,通過從模擬器中刪除應用程序,獲取函數不返回任何記錄。 –