gpt4 book ai didi

ios - 从 CoreData 模块获取数据

转载 作者:行者123 更新时间:2023-11-30 12:28:38 25 4
gpt4 key购买 nike

我正在 Swift 3.0 中开发一个项目,我有一个 CoreData 模块,该模块具有五个字符串类型的属性,以及名为 MP3Details< 的 NSManageObject/。它们分别是持续时间、trackIDtrackNametrackURL

要求是,一旦我传递了 trackID,我就需要检索与该 ID 相关的元素。例如,一旦我传递了 trackID,我应该获得属于该轨道 ID 的轨道持续时间、trackName 等。

到目前为止我得到的是一个空数组。我传递 trackID 并获取属于该 ID 的信息的部分完成代码如下。我在这里缺少什么?

传递trackID的方法

if let arr = StorageManager.getTrackId(playlistName: playlistName) {
for object in arr {
let trackId = object.trackID
print(trackId ?? "ID")

if let detailsArr = StorageManager.getTrackDetails(trackId: trackId) {
print("CoreDataItemArray :", detailsArr)

}
}

是否向CoreData请求详细信息的方法

public static func getTrackDetails(trackId:String?) -> [MP3Details]?{
let appDelegate = UIApplication.shared.delegate as! AppDelegate;
let context = appDelegate.persistentContainer.viewContext;

let request = NSFetchRequest<NSFetchRequestResult>(entityName: MP3_DETAILS_ENTITY)
if(trackId != nil){
request.predicate = NSPredicate(format:"trackID = %@", trackId!)
}
request.returnsObjectsAsFaults = false

do{
let results = try context.fetch(request)
print("Result : ",results)
if(results.count > 0 ){

return results as? [MP3Details]
}else{
print("No Mp3 results for the track ID")
}

}catch{
print("Error in fetching items")
}
return nil;
}

最佳答案

func loadData() -> 无效{

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

let managedObjContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
do {
names = try managedObjContext.fetch(fetchRequest)
} catch let error as NSError {
print("Error while fetching the data:: ",error.description)
}

}

@IBAction func addNewName(_ sender: Any) {
let alert = UIAlertController(title: "Add Name", message: "Add new name:", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "Add", style: UIAlertActionStyle.default) { action in

guard let textField = alert.textFields?.first ,
let nameToSave = textField.text else{
return
}
print("nameToSave::: ",nameToSave)
self.save(name:nameToSave)
self.hitListTableView.reloadData()
}
alert.addTextField()

let alertCancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

alert.addAction(alertAction)
alert.addAction(alertCancelAction)

self.present(alert, animated: true, completion: nil)

}

func save(name:String) -> Void {

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedContext)
let person = NSManagedObject(entity: entity!, insertInto: managedContext)
person.setValue(name, forKey: "name")

do {
try managedContext.save()
names.append(person)
} catch let error as NSError {
print("Error while saving the data: ",error.localizedDescription)
}
}

关于ios - 从 CoreData 模块获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com