2017-06-06 89 views
0

我正在使用swift 3.0開發一個項目,其中有一個名爲「PlayListDetails」的實體的核心數據模塊。這包含兩個子元素名稱playlistName和trackID。因此每個播放列表名稱都有一個與其交叉的trackID。我的要求是當indexPath.row被傳遞時刪除一個特定的元組。雖然我執行方法後實施了一種方法,但它給了我一個例外,說「致命錯誤:索引超出範圍」。我的方法如下:保存數據和我用來刪除數據的方法。從核心數據中的實體中刪除記錄

保存

public static func savePlaylistDetails(audio:[(title:String,healerName:String,trackUrl:String, trackID:String, imageUrl: String)] = [], playListName: String) { 

     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let context = appDelegate.persistentContainer.viewContext 
     let newPlaylist = NSEntityDescription.insertNewObject(forEntityName: PlayList_DETAILS_ENTITY, into: context) 

     let track = audio.map{$0.trackID} 
     print("Track",track) 

     let trackId = track[0] 

     print("ID is :",trackId) 
     newPlaylist.setValue(playListName, forKey:"playlistName") 
     newPlaylist.setValue(trackId, forKey:"trackID") 

     do{ 
      try context.save(); 
      print("Saved.....") 
     } 
     catch{ 
      print("There was an error") 
     } 
    } 

刪除//這是我得到的異常,而我選擇的rowIndex傳遞給函數

func deleteCoreData(rowIndex: Int) { 
    let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
    var playList = [PlayListDetails]() 
    managedContext.delete(playList[rowIndex]) // this is where the exception throws 


    do { 
     try managedContext.save() 

    }catch{ 
     print("fail") 
    } 

} 

我在想什麼?

回答

1

如果您試圖刪除具有特定索引的項目,我提供了在索引處刪除項目的解決方案(這可能會導致數組超出界限的異常),但我建議使用謂詞,可以說可能是id,名稱您願意刪除的項目。

我提供的解決方案與您的問題的感知, 您可以使用從數據庫獲取實體的過程,並刪除您要刪除的特定項目。

請設法添加以下代碼,不要忘了,如果你想刪除一些特定項目

func deleteCoreData(rowIndex: Int) { 

    let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    do{ 
     //1. Create Request 
     let request: NSFetchRequest<PlayListDetails> = PlayListDetails.fetchRequest() 

     //Predicate is optional as per need 
     //let predicate = NSPredicate(format: "playlistId = %@", id) 
     //request.predicate = predicate 

     //2. Result object 
     let results = try managedObjectcontext.fetch(request as! NSFetchRequest<NSFetchRequestResult>) 

     guard results.count > 0 else { 
      print("No tems available handle error") 
      return 
     } 

     for i in 0...results.count-1 { 

      if i == rowIndex { 
       let theItem = results[index] as NSManagedObject 
       managedObjectcontext.delete(item) 
      } 
     } 
     /* 
     for item in results as! [NSManagedObject]{ 
      print(item) 
      //hit delete for item you want 
      managedObjectcontext.delete(item) 
     } */ 

     do { 
      try managedObjectcontext.save() // <- remember to put this :) 

     } catch { 
      // Do something... fatalerror 
      print("handle error") 
      return 
     } 

    } catch{ 
     print(error) 
     print("handle error") 
     return 

    } 
    print("handle error") 

} 

更新

「這是查詢我想要使用謂詞執行先生..從PlayListDetails全部刪除其中trackId = 123和playlistName =演示如何在上述代碼中實現此目標「

使用上刪除部分謂語

//use predicate for single predicate and compound for multiple predicate 
//Predicate is optional as per need 
let predicate = NSPredicate(format: "trackId = %@", id) 
let predicate2 = NSPredicate(format: "name = %@", "some name") 

//let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate, predicate2]) 

    request.predicate = predicate 

使用

for item in results as! [NSManagedObject]{ 
       print(item) 
       //hit delete for item you want 
       managedObjectcontext.delete(item) 
      } 
+0

該刪除的一切,我有 – danu

+0

你必須做謂語,因爲我已經在上面(評論)提供的,與謂詞你有爲您想要刪除的項目提供條件! – dip

+0

一個示例代碼片段將非常讚賞 – danu