Swift 代码正在向核心数据添加不需要的零数据

Posted

技术标签:

【中文标题】Swift 代码正在向核心数据添加不需要的零数据【英文标题】:Swift code is adding unwanted nil data to core data 【发布时间】:2016-10-28 00:44:15 【问题描述】:

我正在尝试在我的应用中包含提醒,并将用户日历添加到我的应用程序中。但是我遇到了一个问题,当我尝试加载保存在核心数据中的数据时,我得到了零。加载几次后我只得到零数据,但每次重新加载应用程序时它都会增加。

addReminderToCalendar 函数

/*
 *
 *This function adds the reminder to the devices calendar and to the devices local storage
 *
 */
func addReminderToCalendar(newReminder : Reminder)
    print("adding reminder :" + newReminder.reminderName)
    /*print(newReminder.reminderDescription)
     print(newReminder.reminderDate)
     print(newReminder.reminderID)
     print(newReminder.reminderName)*/
    if(newReminder.reminderID != nil && newReminder.reminderDate != nil && newReminder.reminderDescription != nil && newReminder.reminderName != nil)
        //print("Adding Reminder to Calendar")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate!
        let managedContext = appDelegate?.managedObjectContext
        let entity =  NSEntityDescription.entity(forEntityName: "Reminders", in:managedContext!)
        let reminderDB = NSManagedObject(entity: entity!, insertInto: managedContext)
        //  print("Reminder ID :" + String(newReminder.reminderID))
        if(checkForReminder(newReminder) == -1)//Checking that the reminder is not already added
            print("addReminderToCalendar :")
            print(newReminder.reminderID)
            reminderDB.setValue(newReminder.reminderID, forKey: "reminderID")
            reminderDB.setValue(newReminder.reminderName, forKey: "reminderName")
            reminderDB.setValue(newReminder.reminderDescription, forKey: "reminderDescription")
            reminderDB.setValue(newReminder.reminderDate, forKey: "reminderDate")
            reminders.append(newReminder)
        
        if (managedContext?.hasChanges)! 
            do 
                try managedContext?.save()
             catch 
                //print(newReminder)
                NSLog("Error Here")
            
        
    else
        print("The reminder to be added was nil")
    

加载提醒功能

    /*
 * This function loads the reminders that have been stored in the device
 */
func loadReminders()

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Reminders")

    let results = try? managedContext.fetch(fetchRequest)

    storedReminders = results as! [NSManagedObject]
    print(storedReminders.count)
    //print(storedReminders[0].value(forKey: "reminderID"))
    if (storedReminders.count > 0)
        for index in 0 ... storedReminders.count - 1
            print("Loading reminder with ID : ")
            print(storedReminders[index].value(forKey: "reminderID"))
            print("Loading reminder with Name : ")
            print(storedReminders[index].value(forKey: "reminderName"))

            if(storedReminders[index].value(forKey: "reminderID") != nil && storedReminders[index].value(forKey: "reminderName") != nil && storedReminders[index].value(forKey: "reminderDescription") != nil && storedReminders[index].value(forKey: "reminderDate") != nil) //checking that the values are not null
                let loadedReminder = Reminder()
                loadedReminder.reminderID = storedReminders[index].value(forKey: "reminderID") as! Double
                loadedReminder.reminderName = storedReminders[index].value(forKey: "reminderName") as! String
                loadedReminder.reminderDescription = storedReminders[index].value(forKey: "reminderDescription") as! String
                loadedReminder.reminderDate = (storedReminders[index].value(forKey: "reminderDate") as! NSDate) as Date!
                reminders.append(loadedReminder)
            
        
    

提醒课

import Foundation
import UIKit
import CoreData

class Reminder :UICollectionViewCell

   var reminderDate: Date!
   var reminderDescription: String!
   var reminderID: Double!
   var reminderName: String!


编辑 我在负载提醒中添加了以下几行,这是为了阻止核心数据被 nil 值填充。 别的 //删除这个提醒 managedContext.delete(storedReminders[index] as NSManagedObject) print("删除的数据") 所以我的 loadReminders 函数现在看起来像这样

/* * 此功能加载已存储在设备中的提醒 */ func loadReminders()

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext

let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Reminders")

let results = try? managedContext.fetch(fetchRequest)

storedReminders = results as! [NSManagedObject]
print(storedReminders.count)
//print(storedReminders[0].value(forKey: "reminderID"))
if (storedReminders.count > 0)
    for index in 0 ... storedReminders.count - 1
        print("Loading reminder with ID : ")
        print(storedReminders[index].value(forKey: "reminderID"))
        print("Loading reminder with Name : ")
        print(storedReminders[index].value(forKey: "reminderName"))

        if(storedReminders[index].value(forKey: "reminderID") != nil && storedReminders[index].value(forKey: "reminderName") != nil && storedReminders[index].value(forKey: "reminderDescription") != nil && storedReminders[index].value(forKey: "reminderDate") != nil) //checking that the values are not null
            let loadedReminder = Reminder()
            loadedReminder.reminderID = storedReminders[index].value(forKey: "reminderID") as! Double
            loadedReminder.reminderName = storedReminders[index].value(forKey: "reminderName") as! String
            loadedReminder.reminderDescription = storedReminders[index].value(forKey: "reminderDescription") as! String
            loadedReminder.reminderDate = (storedReminders[index].value(forKey: "reminderDate") as! NSDate) as Date!
            reminders.append(loadedReminder)
        else
                //delete this reminder
                managedContext.delete(storedReminders[index] as NSManagedObject)
                print("deleted data")
            
    

【问题讨论】:

【参考方案1】:

在这里,您正在创建一个新的 Reminders 实例并将其添加到 NSManagedObjectContext

let entity =  NSEntityDescription.entity(forEntityName: "Reminders", in:managedContext!)
let reminderDB = NSManagedObject(entity: entity!, insertInto: managedContext)

然后你检查它是否已经添加:

if(checkForReminder(newReminder) == -1)

这就是我认为的问题所在。您已经将 Reminders 实例添加到您的上下文中!所以managedContext?.hasChanges 将是true

上下文发生了变化,这些Reminders 将与您的所有实体属性及其默认值一起保存。我假设他们默认为nil

这应该可以解决它,尽管我看不到您的部分代码,所以请谨慎对待...

func addReminderToCalendar(newReminder: Reminder) 
    if(newReminder.reminderID != nil && newReminder.reminderDate != nil && newReminder.reminderDescription != nil && newReminder.reminderName != nil) 
        if(checkForReminder(newReminder) == -1) 
            let appDelegate = UIApplication.shared.delegate as! AppDelegate!
            let managedContext = appDelegate?.managedObjectContext
            let entity =  NSEntityDescription.entity(forEntityName: "Reminders", in:managedContext!)
            let reminderDB = NSManagedObject(entity: entity!, insertInto: managedContext)
            reminderDB.setValue(newReminder.reminderID, forKey: "reminderID")
            reminderDB.setValue(newReminder.reminderName, forKey: "reminderName")
            reminderDB.setValue(newReminder.reminderDescription, forKey: "reminderDescription")
            reminderDB.setValue(newReminder.reminderDate, forKey: "reminderDate")
            reminders.append(newReminder)

            if (managedContext?.hasChanges)! 
                do 
                    try managedContext?.save()
                 catch 
                    NSLog("Error Here")
                
            
        
     else 
        print("The reminder to be added was nil")
    

【讨论】:

以上是关于Swift 代码正在向核心数据添加不需要的零数据的主要内容,如果未能解决你的问题,请参考以下文章

Swift 核心数据更改 bool 存储

保存到多个实体中 - 核心数据 - Swift

向核心数据实体添加瞬态属性是不是需要新版本模型?

Swift - 使用 for 循环将数据添加到核心数据

在哪里为 Swift 中的核心数据实现 NSValueTransformer

Swift:向图像添加文本不起作用