gpt4 book ai didi

ios - 关闭和打开应用程序后核心数据约束工作错误(Swift)

转载 作者:行者123 更新时间:2023-11-28 06:34:42 25 4
gpt4 key购买 nike

我正在完成我的作业。基本上,该应用程序将继续跟踪我所做的事件。我正在使用核心数据来保持跟踪记录,约束应用于标题,因为我不想跟踪相同的事件(核心数据的图片如下所示) http://i.stack.imgur.com/qEySx.jpg

当我第一次运行时,一切正常。但是,在我关闭应用程序并再次运行后,约束在我添加全新事件时出错,但它始终显示该事件已存在于核心数据中。

奇怪的是它显示了其他实体(俚语)的 NSConstraintConflict,如下所示:

Track already existed in coredata Error Domain=NSCocoaErrorDomain Code=133021 "(null)" UserInfo={conflictList=( "NSConstraintConflict (0x7f9721c9cf00) for constraint (\n word\n): database: (entity: Slang; id: 0xd000000000040000 ; data: ), conflictedObjects: (\n \" (entity: Slang; id: 0xd0000000018c0000 ; data: {\n explain = \\"Excellent! Very good!\\";\n pronunciation = \\"/e\\U026as/\\";\n pronunciationMp3 = ace;\n word = Ace;\n})\"\n)" )}, [conflictList: <__NSArrayM 0x7f972401eaf0>( NSConstraintConflict (0x7f9721c9cf00) for constraint ( word ): database: (entity: Slang; id: 0xd000000000040000 ; data: ), conflictedObjects: ( " (entity: Slang; id: 0xd0000000018c0000 ; data: {\n explain = \"Excellent! Very good!\";\n pronunciation = \"/e\U026as/\";\n pronunciationMp3 = ace;\n word = Ace;\n})" ) ) ]

这是我用来将轨道保存到核心数据中的代码:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Track", inManagedObjectContext:context)
let newTrack = Track(entity:entity!, insertIntoManagedObjectContext: context)
do {
newTrack.title = self.thisActivity.title!
newTrack.star = self.thisActivity.star!
newTrack.dodate = NSDate()
newTrack.webpic = UIImagePNGRepresentation(self.thisActivity.picture!)!
newTrack.uptext = ""
newTrack.uppic = ""

try context.save()
SweetAlert().showAlert("Done!", subTitle: "Save to your track!", style: AlertStyle.Success)
} catch let error as NSError {
print("Track already existed in coredata \(error), \(error.userInfo)")
SweetAlert().showAlert("Sorry!", subTitle: "You have saved before!", style: AlertStyle.Success)
context.deleteObject(newTrack)
}

期待大家的解答

最佳答案

您有一个约束,规定 word 字段必须是唯一的。这意味着当您插入新轨道时,您需要使用新词或删除旧记录。这里有很多信息:http://dorianroy.com/blog/2015/09/how-to-implement-unique-constraints-in-core-data-with-ios-9/

关于ios - 关闭和打开应用程序后核心数据约束工作错误(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39366519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com