为什么字符串数组可能无法正确存储在Core Data中?

时间:2018-11-30 02:46:05

标签: ios swift core-data nsmanagedobject

我的情况是,我有多个警报,每个警报都有一组通知UUID,这些UUID充当警报所负责的通知的​​标识符。创建警报时,我在“警报”对象中设置了这些UUID。它会检查我可以创建多少个通知(一次最多可以创建64个通知,在您开始摆脱其他通知之前),并创建1个(对于今天或明天的警报)或尽可能多(对于每日警报;我知道我必须将其更改为更低的水平,否则每天只发出一个警报是可持续的,我不希望这样)。

好的,所以我将这些警报存储在Core Data Managed Objects中,就像在saveAlarm()中一样:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
    }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Alarm", in: managedContext)!
let alarmMO = AlarmMO(entity: entity, insertInto: managedContext)

alarmMO.setValue(alarmToSave.alarmTime, forKeyPath: "alarmTime")
alarmMO.setValue(alarmToSave.alarmNumber, forKeyPath: "alarmNumber")
alarmMO.setValue(alarmToSave.alarmIntervalBeginTimeDouble, forKeyPath: "startTimeInterval")
alarmMO.setValue(alarmToSave.alarmIntervalEndTimeDouble, forKeyPath: "endTimeInterval")
alarmMO.setValue(alarmToSave.recurrence.hashValue, forKeyPath: "recurrence")
alarmMO.setValue(alarmToSave.notificationUuids, forKeyPath: "notificationUuids")

if managedContext.hasChanges {
    do {
        try managedContext.save()
        self.alarms.append(alarmMO)
    } catch let error as NSError {
        print("Could not save alarm to CoreData. \(error), \(error.userInfo)")
    }
} else {
    os_log("No changes to the context to save", log: OSLog.default, type: .debug)
}

在我的.xcdatamodeld文件中,我将NotificationUuids存储为Transformable对象类型,并且Custom Class属性为[String]

.xcdatamodeld file

这是很奇怪的事情:如果我仅将1条警报保存到Core Data中,则此设置有效。当我去删除removeNotifications()中的通知时,我想删除仍排定的具有附加到此Alarm对象的notificationUuid的通知。如果仅创建了一个通知(在今天或明天的警报中),则它起作用;但是,如果创建了多个通知(在每日警报的情况下),则不起作用。我知道我的核心数据实现存在问题,因为这些notificationUuids的来源是托管对象:

// Up here I call notificationCenter.getPendingNotificationRequests(). All of this code is wrapped by this call.
for request in requests {
    activeNotificationUuids.append(request.identifier)
}
for alarm in self.alarms {
    guard let alarmUuids = alarm.value(forKey: "notificationUuids") as! [String]? else {
        os_log("Found nil when attempting to unwrap notificationUuids in deleteOldAlarms() in AlarmTableViewController.swift, cancelling",
                   log: OSLog.default, type: .default)
        return
    }
    let activeNotificationUuidsSet: Set<String> = Set(activeNotificationUuids)
    let alarmUuidsSet: Set<String> = Set(alarmUuids)
    let union = activeNotificationUuidsSet.intersection(alarmUuidsSet)
    if union.isEmpty {
        alarmsToDelete.append(alarm)
    }
}
for alarmMOToDelete in alarmsToDelete {
    self.removeNotifications(notificationUuids: alarmMOToDelete.notificationUuids)
    managedContext.delete(alarmMOToDelete)
    self.alarms.removeAll { (alarmMO) -> Bool in
        return alarmMOToDelete == alarmMO
    }
}

self.alarms中的警报是AlarmMO对象,其定义如下:

import CoreData

class AlarmMO: NSManagedObject {

    @NSManaged var alarmTime: Date?
    @NSManaged var alarmNumber: Int
    @NSManaged var startTimeInterval: Double
    @NSManaged var endTimeInterval: Double
    @NSManaged var recurrence: Int
    @NSManaged var notificationUuids: [String]

}

有什么想法吗?不能将数组作为一个数组正确存储,而不能作为一个单独的String正确存储,如果提供多个​​数组,则会失败?

0 个答案:

没有答案