我从关系中获取数据时遇到问题。 我有一个调查,我想将答案,userIds和结果保存到核心数据,并在下次启动应用程序时获取数据。 它们在阵列中。 如果它是结果,则每个都将转换为NSManagedObject“Wert”,转换为属性“valueInt”,如果是answer或userId,则转换为“valueString”。
extension Wert {
@NSManaged var valueInt: NSNumber?
@NSManaged var valueString: String?
}
之后它将保存在NSManagedObject“Message”上,作为答案,结果或userIds上的NSSet。
extension Message {
@NSManaged var absenderId: String?
@NSManaged var absenderName: String?
@NSManaged var datum: NSDate?
@NSManaged var gruppenId: String?
@NSManaged var image: NSData?
@NSManaged var latitude: NSNumber?
@NSManaged var longitude: NSNumber?
@NSManaged var messageId: String?
@NSManaged var question: String?
@NSManaged var sound: NSData?
@NSManaged var text: String?
@NSManaged var answers: NSSet?
@NSManaged var results: NSSet?
@NSManaged var userIDs: NSSet?
}
我认为这是有效的,因为在“addObjectsFromArray”之后AnswersSet包含了一些值。
func saveCoreData(AbsenderName: String, AbsenderID: String, Text: String?, Image: NSData?, Datum: NSDate, Latitude: Double?, Longitude: Double?, MessageId: String, Sound: NSData?, question: String?, answers : [String]?, results : [Int]?, userIDs: [String]?) {
let newMessage = NSEntityDescription.insertNewObjectForEntityForName("Message", inManagedObjectContext: context) as NSManagedObject
newMessage.setValue(AbsenderID, forKey: "absenderId")
newMessage.setValue(AbsenderName, forKey: "absenderName")
newMessage.setValue(Text, forKey: "text")
newMessage.setValue(Image, forKey: "image")
newMessage.setValue(Latitude, forKey: "latitude")
newMessage.setValue(Longitude, forKey: "longitude")
newMessage.setValue(Datum, forKey: "datum")
newMessage.setValue(GroupId, forKey: "gruppenId")
newMessage.setValue(MessageId, forKey: "messageId")
newMessage.setValue(Sound, forKey: "sound")
if question != nil && answers != nil && results != nil && userIDs != nil {
newMessage.setValue(question, forKey: "question")
var AnswersArray = [NSManagedObject]()
var ResultsArray = [NSManagedObject]()
var userIDsArray = [NSManagedObject]()
for var index = 0; index < answers?.count ; ++index {
let newWert = NSEntityDescription.insertNewObjectForEntityForName("Wert", inManagedObjectContext: context) as NSManagedObject
newWert.setValue(answers![index], forKey: "valueString")
AnswersArray.append(newWert)
}
for var index = 0; index < results?.count ; ++index {
let newWert = NSEntityDescription.insertNewObjectForEntityForName("Wert", inManagedObjectContext: context) as NSManagedObject
newWert.setValue(results![index], forKey: "valueInt")
ResultsArray.append(newWert)
}
for var index = 0; index < userIDs?.count ; ++index {
let newWert = NSEntityDescription.insertNewObjectForEntityForName("Wert", inManagedObjectContext: context) as NSManagedObject
newWert.setValue(userIDs![index], forKey: "valueString")
userIDsArray.append(newWert)
}
let answersSet = newMessage.mutableSetValueForKey("answers")
let resultsSet = newMessage.mutableSetValueForKey("results")
let userIdsSet = newMessage.mutableSetValueForKey("userIDs")
answersSet.addObjectsFromArray(AnswersArray)
resultsSet.addObjectsFromArray(ResultsArray)
userIdsSet.addObjectsFromArray(userIDsArray)
}
do {
try context.save()
}
catch _ {
print("Error")
}
}
但是当我尝试使用mutableSetValueForKey()获取保存的值时,它们包含0个对象。其他一切都在发挥作用。
func loadCoreData() -> Int {
var x : [AnyObject] = [AnyObject]()
let request = NSFetchRequest(entityName: "Message")
request.resultType = NSFetchRequestResultType.DictionaryResultType
request.predicate = NSPredicate(format: "gruppenId = %@", GroupId)
let sort1 = NSSortDescriptor(key: "datum", ascending: true)
request.sortDescriptors = [sort1]
do {
x = try context.executeFetchRequest(request)
for (var i = 0 ; i < x.count; ++i ) {
let Absender = x[i].valueForKey("absenderName") as! String
let AbsenderID = x[i].valueForKey("absenderId") as! String
let Text : String? = x[i].valueForKey("text") as? String
let Image : NSData? = x[i].valueForKey("image") as? NSData
let Sound : NSData? = x[i].valueForKey("sound") as? NSData
let Date : NSDate = x[i].valueForKey("datum") as! NSDate
let GruppenID : String = x[i].valueForKey("gruppenId") as! String
let MessageID : String = x[i].valueForKey("messageId") as! String
let longitude : Double? = x[i].valueForKey("longitude") as? Double
let latitude : Double? = x[i].valueForKey("latitude") as? Double
let question : String? = x[i].valueForKey("question") as? String
let answers = x[i].mutableSetValueForKey("answers") as? NSMutableSet
let results = x[i].mutableSetValueForKey("results") as? NSMutableSet
let userIds = x[i].mutableSetValueForKey("userIDs") as? NSMutableSet
}
}
}
答案 0 :(得分:0)
似乎答案,结果等是一对多的关系。在这种情况下,简单地设置一对一关系总是更安全(也更简洁)。
因此,您可以简单地使用反向关系,而不是维护一个笨拙的数组并处理mutableSetValueForKey
:
newWert.message = newMessage
注意:请采用使用lowerCase
变量名称的约定,以避免与类名混淆。
NB2:我强烈建议使用NSManagedObject
子类并使用dot.notation来设置和获取值。只需一个笔划,您在上面的许多代码行中定义的所有变量都将变得完全多余。