Swift核心数据,记录不会删除,即使使用save()

时间:2017-12-21 12:02:19

标签: swift core-data

我正在尝试从swift中删除核心数据中的对象。我可以保存对象,但删除不起作用。我正在调用.delete(对象)和.save(),它们正确运行并验证了被输入的对象是有效的,但是尽管没有从代码中返回错误,但对象永远不会被删除。

这是我的保存功能,当false作为参数提供时,它应该删除记录:

func SaveFighter(_ save:Bool)
{
    // get the (only) managed context...
    let managedContext = DataBaseHelper.persistentContainer.viewContext
    // ... get the Entity (table) description for the only entity [SavedFighter]...
    let entity = NSEntityDescription.entity(forEntityName: "SavedFighter", in: managedContext)!
    // ... retrieve the associated Maanaged Object for this entity
    let savedFighter = NSManagedObject(entity: entity, insertInto: managedContext)

    // put Fighter object members into matching attributes in the Managed Object
    savedFighter.setValue(self.nickname, forKeyPath: "nickname")
    savedFighter.setValue(self.first_name, forKeyPath: "first_name")
    savedFighter.setValue(self.last_name, forKeyPath: "last_name")
    savedFighter.setValue(self.weight_class, forKeyPath: "weight_class")
    savedFighter.setValue(self.statid, forKeyPath: "statid")
    savedFighter.setValue(self.losses, forKeyPath: "losses")
    savedFighter.setValue(self.draws, forKeyPath: "draws")
    savedFighter.setValue(self.wins, forKeyPath: "wins")
    savedFighter.setValue(self.id, forKeyPath: "id")
    savedFighter.setValue(self.title_holder, forKeyPath: "title_holder")
    savedFighter.setValue(self.fighter_status, forKeyPath: "fighter_status")
    savedFighter.setValue(self.rank, forKeyPath: "rank")
    savedFighter.setValue(self.thumbnail, forKeyPath: "thumbnail")

    if (save) // save this fighter in DB
    {
        // need to account for exceptions if saving fails
        do
        {
            // commit the prepares Managed Object in the Managed Context
            try managedContext.save()
            // set flag to say the object has been saved to DB
            print("Fighter: \(first_name) was ADDED to the Bookmarks Database!")
        }
        catch let error as NSError
        {
            print("Could not save \(first_name) \(error)")
        }
    }
    else  // delete this fighter from bookmarks DB
    {
        // create a fetchrequest to associate with the SavedFighter entity
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "SavedFighter")
        // set a predicate (query) to only select a unique record based on id
        print("Attempting to delete record with ID: " + String(self.id))

        fetchRequest.predicate = NSPredicate(format: "id = %@", String(self.id))   // use id because it is unique

        do
        {
            // run the query to get the resultset (can be more than one record)
            let resultData = try managedContext.fetch(fetchRequest) as! [SavedFighter]
            print("Found \(resultData.count) records to delete...")
            // iterate through all the found records...
            for object in resultData
            {   //... to delete each record
                print(object)
                managedContext.delete(object)
                try managedContext.save() // commit changes to context
            }
        }
        catch
        {
            print("Could not find any records to delete...")
        }
    }
}

我期待在调用Fighter.SaveFighter(false)时从DB中删除对象,但无论如何都会保留。

从调试开始,我得到了以下打印件:

Attempting to delete record with ID: 1161
Found 1 records to delete...
<SavedFighter: 0x61800028c170> (entity: SavedFighter; id: 0xd000000000bc0000 <x-coredata://64D235C6-E488-4EC1-ADA6-9F4B7CBCAB0D/SavedFighter/p47> ; data: <fault>)

因此,该对象显然已成功从数据库中检索,但仍不会删除。

0 个答案:

没有答案