swift – UNIQUE约束失败:ZTEMPORADA.Z_PK

swift – UNIQUE约束失败:ZTEMPORADA.Z_PK,第1张

概述我正在研究 swift和CoreData,但是在与“Temporada”进行“时间”关系时遇到问题,一个团队可以有几个赛季,一个赛季就是一个团队. 上课时间 import Foundationimport CoreData@objc(Time)class Time: NSManagedObject {@NSManaged var nome: String@NSManaged var 我正在研究 swift和CoreData,但是在与“Temporada”进行“时间”关系时遇到问题,一个团队可以有几个赛季,一个赛季就是一个团队.

上课时间

import Foundationimport CoreData@objc(Time)class Time: NSManagedobject {@NSManaged var nome: String@NSManaged var temporada: NSSetoverrIDe init(entity: NSEntityDescription,insertIntoManagedobjectContext context: NSManagedobjectContext?) {    let entity:NSEntityDescription = CoreDataManager.getEntity("Time")    super.init(entity: entity,insertIntoManagedobjectContext: nil)}class func entityDescription() -> (NSEntityDescription){    let entity:NSEntityDescription = CoreDataManager.getEntity("Time")    return entity}func salvar(){    let context:NSManagedobjectContext = CoreDataManager.getContext()    var error:NSError?    if (!self.inserted) {        context.insertObject(self)    }    context.save(&error)    if (error != nil){        NSLog(error!.description)    }}}

Temporada类

import Foundationimport CoreData@objc(Temporada)class Temporada: NSManagedobject {@NSManaged var ano: NSNumber@NSManaged var numeroJogos: NSNumber//relacionamentos@NSManaged var time: Time@NSManaged var jogador_temporada: NSSetoverrIDe init(entity: NSEntityDescription,insertIntoManagedobjectContext context: NSManagedobjectContext?) {    let entity:NSEntityDescription = CoreDataManager.getEntity("Temporada")    super.init(entity: entity,insertIntoManagedobjectContext: nil)}class func entityDescription() -> (NSEntityDescription){    let entity:NSEntityDescription = CoreDataManager.getEntity("Temporada")    return entity}func salvar(){    let context:NSManagedobjectContext = CoreDataManager.getContext()    var error:NSError?    if (!self.inserted) {        context.insertObject(self)    }    context.save(&error)    if (error != nil){        NSLog(error!.description)    }}    }

CoreDataManager

import Foundation import CoreData import UIKit class CoreDataManager {class func getEntity(entIDade: String) -> NSEntityDescription {    let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate    let context:NSManagedobjectContext? = delegate.managedobjectContext    let description: NSEntityDescription = NSEntityDescription.entityForname(entIDade,inManagedobjectContext: context!)!    return description}class func getContext() -> NSManagedobjectContext {    let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate    return delegate.managedobjectContext!}class func getAllManagedobjectsFromEntity(entity: NSEntityDescription) -> (sucesso: Bool,objects: NSArray){    let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate    let context:NSManagedobjectContext? = delegate.managedobjectContext    let request:NSFetchRequest = NSFetchRequest()    request.entity = entity    var error:NSError?    var objects:NSArray? = context?.executeFetchRequest(request,error: &error)    if(error == nil){        return(true,objects!)    }else{        NSLog(error!.description)        return(false,objects!)    }}}

在我的VIEwController中,我有两个方法,一个用于保存“时间”,另一个用于建立这种关系……

@IBAction func salvar(sender: AnyObject) {    var time: Time  = Time(entity: Time.entityDescription(),insertIntoManagedobjectContext: nil)    time.nome = "Cruzeiro Esporte Clube"    time.salvar()    NSLog("Time Salvo")}

@IBAction func salvarTemporada(sender:AnyObject){

var time: Time?    var temporada: Temporada = Temporada(entity: Temporada.entityDescription(),insertIntoManagedobjectContext: nil)    temporada.ano = NSNumber(integer: 2017)    temporada.numeroJogos = NSNumber(integer: 10)    temporada.salvar()    var retorno = CoreDataManager.getAllManagedobjectsFromEntity(Time.entityDescription())    if (retorno.sucesso) {        time = retorno.objects.objectAtIndex(0) as? Time        NSLog("recuperado = \(time?.nome)")    }    temporada.time = time!    temporada.salvar() }

但是我收到以下错误

2015-08-23 14:36:46.718 Kickoff_CoreData[1230:37874] CoreData: error: (1555) UNIQUE constraint Failed: ZTEMPORADA.Z_PK2015-08-23 14:36:46.719 Kickoff_CoreData[1230:37874] Core Data: error: -executeRequest: encountered exception = error during sql execution : UNIQUE constraint Failed: ZTEMPORADA.Z_PK with userInfo = {    NSfilePath = "/Users/jonathanribeiro/library/Developer/CoreSimulator/Devices/79DFD839-4790-40C4-8E92-59EB9ADED5CD/data/Containers/Data/Application/1CEB63B6-3AA8-414D-810F-46EEDC910270/documents/Kickoff_CoreData.sqlite";    NSsqliteErrorDomain = 1555;}

谁能帮我?

我最近遇到了类似的问题.我发现我在NSManagedobjectContext中插入NSManagedobject两次,然后尝试保存它.我看到你在同一个方法中调用了两次temporada.salvar,并且由于保存实际上是异步的,因此可能会造成麻烦.希望能帮助到你. 总结

以上是内存溢出为你收集整理的swift – UNIQUE约束失败:ZTEMPORADA.Z_PK全部内容,希望文章能够帮你解决swift – UNIQUE约束失败:ZTEMPORADA.Z_PK所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1043910.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存