2017-05-09 95 views
0

我正在Swift 3.0中進行項目工作,並且我有一個CoreData模塊,它具有String類型的五個屬性,並且NSManageObject的名稱爲MP3Details。他們是,持續時間,trackID,trackName,trackURL從CoreData模塊獲取數據

要求是,一旦我通過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; 
} 
+0

不,您不能使用上面的代碼swift 3.0訪問核心數據。核心數據的訪問發生了迅速變化。您可以直接訪問核心數據persistentContainer。 – phani

+0

代碼片段將非常感激,因爲事情是當我通過一個nill值,而不是trackID我得到Mp3Details – danu

+1

到目前爲止,我沒有在迅速工作。但我有這個代碼。等待我會檢查它。 – phani

回答

0

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) 
    } 
} 
+0

謂詞在哪裏?這不符合我的要求 – danu

+0

好的。我會再次檢查代碼。 2分鐘 – phani

+0

Sry yar。在我的mac我有代碼。但在谷歌驅動器代碼丟失。 – phani

0

嘗試更換謂詞符合:

request.predicate = NSPredicate(format:"%K == %@", "trackID", trackId!)