Reputation: 61
I am working through a Core Data tutorial and my code throws the following error.
Could not cast value of type 'NSAsynchronousFetchResult' (0x103e13388) to 'NSArray' (0x10435cf28). 2018-12-29 22:54:17.619639+0700 demoCoreData[2670:247527] Could not cast value of type 'NSAsynchronousFetchResult' (0x103e13388) to 'NSArray' (0x10435cf28). (lldb)
My Question is:
Based on the error provided what is the reason my application is failing ?
Here's my code that won't compile:
import UIKit
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var txtID: UITextField!
@IBOutlet weak var txtTen: UITextField!
@IBOutlet weak var txtTuoi: UITextField!
@IBOutlet weak var lblKetQua: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func btnInsert(_ sender: Any) {
let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate // tạo biến để connect giữa iphone với Manage Object Context
let context: NSManagedObjectContext = appDel.persistentContainer.viewContext //tạo biến connect với db
//insert new Khach Hang
let newKH = NSEntityDescription.insertNewObject(forEntityName: "KhachHang", into: context) //insert new object
newKH.setValue(Int(txtID.text!), forKey: "id") //forKey = tên column
newKH.setValue(txtTen.text, forKey: "tenKH")
newKH.setValue(Int(txtTuoi.text!), forKey: "tuoiKH")
do {
try context.save()
print("Insert Success")
}catch {
let err = error as NSError
print("Error is: \(err)")
}
}
@IBAction func btnShow(_ sender: Any) {
let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate // tạo biến để connect giữa iphone với Manage Object Context
let context: NSManagedObjectContext = appDel.persistentContainer.viewContext // tạo biến connect với db
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "KhachHang")// tạo biến truy vấn đến table "KhachHang"
request.returnsObjectsAsFaults = false // có trả về những kết quả sai hay ko
do {
let results = try context.execute(request)
print("Truy Van Success")
for re in results as! [NSManagedObject] {
print(re.value(forKey: "tenKH"))
}
}catch {
let err = error as NSError
print("Excute err: \(err)")
}
}
}
Here is my *.xcdatamodeld file
https://drive.google.com/open?id=18b1iJYz3jWAFkxYK2V4RrkxCTeWuR5yr
What I would really like to know, is how to investigate this on my own. Thanks in advance for any help!
Upvotes: 4
Views: 915
Reputation: 70976
From your code:
let results = try context.execute(request)
for re in results as! [NSManagedObject] {
print(re.value(forKey: "tenKH"))
}
The execute
method you're using returns an instance of NSPersistentStoreResult
. This is an abstract class. The NSAsynchronousFetchResult
mentioned in your error message is a concrete subclass of NSPersistentStoreResult
.
Next, you attempt to cast this to [NSManagedObject]
, which is a different, unrelated class. The error you see happens because it's not possible to typecast an NSAsynchronousFetchResult
to an array, because they are not related or even similar to each other.
It looks like you meant to use fetch(request)
instead of execute(request)
, because fetch
returns an array of managed objects.
What I would really like to know, is how to investigate this on my own.
I wouldn't mention it but since you asked specifically: This is something you'd resolve by looking at Apple's documentation for the classes and methods you're using. The execute
method is documented to return something different from what your code expects, and the error message gives a big clue to the nature of the problem.
Upvotes: 4