Ios 从核心数据中提取的数据缺少某些信息
我的核心数据模型中有两个实体:CardCollection(类名CardCollectionMO)和Card(CardMO)。卡片收藏可以有许多卡片。这是我用一套新卡保存CardCollection的代码,它似乎工作正常Ios 从核心数据中提取的数据缺少某些信息,ios,swift,core-data,nsfetchrequest,Ios,Swift,Core Data,Nsfetchrequest,我的核心数据模型中有两个实体:CardCollection(类名CardCollectionMO)和Card(CardMO)。卡片收藏可以有许多卡片。这是我用一套新卡保存CardCollection的代码,它似乎工作正常 func saveCardInfo() { let currentDate = createDate() let collectionNumber = arc4random_uniform(1000000) let cardcollection =
func saveCardInfo() {
let currentDate = createDate()
let collectionNumber = arc4random_uniform(1000000)
let cardcollection = CardCollectionMO.insertNewCollection(collectionNumber: Int64(collectionNumber), collectionTitle: collectionTitle.text!, collectionDescription: collectionDescription.text!, numberOfCards: Int16(cards.count), dateCreated: String(currentDate))
var newCard = Set<CardMO>()
for card in cards {
if let addedCard = CardMO.insertNewCard(questionText: card.questionText, answerText: card.answerText, cardNum: Int16(card.cardNum), questionImage: UIImageJPEGRepresentation(card.questionImage, 1)! as NSData) {
addedCard.cardcollection = cardcollection
newCard.insert(addedCard)
}
}
cardcollection?.addToCards(newCard as NSSet)
}
func saveCardInfo(){
让currentDate=createDate()
让collectionNumber=arc4random_均匀(1000000)
让cardcollection=CardCollectionMO.insertNewCollection(collectionNumber:Int64(collectionNumber),collectionTitle:collectionTitle.text!,collectionDescription:collectionDescription.text!,numberOfCards:Int16(cards.count),dateCreated:String(currentDate))
var newCard=Set()
卡片中的卡片{
如果让addedCard=CardMO.insertNewCard(问题文本:card.questionText,回答文本:card.answerText,cardNum:Int16(card.cardNum),问题图像:UIImageJPEG表示(card.questionImage,1)!作为NSData){
addedCard.cardcollection=cardcollection
新卡.插入(添加卡)
}
}
cardcollection?.addToCards(新卡作为NSSet)
}
但是,取回数据时会发生奇怪的事情。这是密码
func fetchData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<CardCollectionMO>(entityName: "CardCollection")
do {
let results = try managedObjectContext.fetch(request)
if results.count > 0 {
for result in results {
if let collectionTitle = result.collectionTitle {
print(collectionTitle)
}
if let listOfCards = result.cards?.allObjects as? [CardMO] {
for card in listOfCards {
if let questionText = card.questionText {
print(questionText)
}
}
func fetchData(){
让appDelegate=UIApplication.shared.delegate为!appDelegate
让managedObjectContext=appDelegate.persistentContainer.viewContext
let request=NSFetchRequest(entityName:“CardCollection”)
做{
let results=try managedObjectContext.fetch(请求)
如果results.count>0{
为了得到结果{
如果让collectionTitle=result.collectionTitle{
印刷品(收藏品名称)
}
如果let listOfCards=result.cards?.allObjects as?[CardMO]{
卡片列表中的卡片{
如果让questionText=card.questionText{
打印(问题文本)
}
}
当应用程序运行时,上面的代码可以正常工作,但是如果我停止并重新启动应用程序,每个集合中的最后一张卡都会被删除。我已经完成了一个listOfCards.count,它好像不存在。但是,如果我只是从卡实体中取出所有卡,它们就在那里。有没有想过我做错了什么,以及为什么集合中的最后一张卡不存在正在提取?试试这个,这是一个有效的提示:如果您创建了一个反向关系,那么设置关系的一面就足够了,另一面是自动设置的。您是否验证了每张卡是否正确存储在
saveCardInfo()
(CardMO.insertNewCard
对每张卡都成功)?