这里不允许使用CoreDatato-many键的关系

时间:2016-05-03 16:40:27

标签: ios core-data

我遇到了与核心数据关系相关的崩溃,这种关系是多对的

nueva marca -----><Autos.Marca: 0x7fac604d3250> (entity: Marca; id:
0x7fac604d3300
<x-coredata:///Marca/tB1CE1C53-077E-4402-8E3A-E6C6F147B4672> ; data: {
    modelos =     (
    );
    nombre = NIKE; }) 

Nuevo modelo:::::::: <Autos.Modelo: 0x7fac604d6df0> (entity: Modelo;
id: 0x7fac604d6e50
<x-coredata:///Modelo/tB1CE1C53-077E-4402-8E3A-E6C6F147B4673> ; data:
{
    marca = "0x7fac604d3300 <x-coredata:///Marca/tB1CE1C53-077E-4402-8E3A-E6C6F147B4672>";
    nombre = "AIRFORCE 1"; }) arreglo Relationship 'modelos' on managed object (0x7fac604d3250) <Autos.Marca: 0x7fac604d3250> (entity:
Marca; id: 0x7fac604d3300
<x-coredata:///Marca/tB1CE1C53-077E-4402-8E3A-E6C6F147B4672> ; data: {
    modelos =     (
        "0x7fac604d6e50 <x-coredata:///Modelo/tB1CE1C53-077E-4402-8E3A-E6C6F147B4673>"
    );
    nombre = NIKE; }) with objects {(
    <Autos.Modelo: 0x7fac604d6df0> (entity: Modelo; id: 0x7fac604d6e50> <x-coredata:///Modelo/tB1CE1C53-077E-4402-8E3A-E6C6F147B4673> ; data:
{
    marca = "0x7fac604d3300 <x-coredata:///Marca/tB1CE1C53-077E-4402-8E3A-E6C6F147B4672>";
    nombre = "AIRFORCE 1"; }) )} Nuevo Marca <Autos.Marca: 0x7fac604d3250> (entity: Marca; id: 0xd000000000080000
<x-coredata://D8BC4DF2-5A4A-4D3D-A10D-91FE28DABA30/Marca/p2> ; data: {
    modelos =     (
        "0xd000000000080002 <x-coredata://D8BC4DF2-5A4A-4D3D-A10D-91FE28DABA30/Modelo/p2>"
    );
    nombre = NIKE; }) 2016-05-03 11:21:22.778 Autos[745:17150] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'to-many key not allowed here'
*** First throw call stack: (   0   CoreFoundation 0x000000010bbc7d85 __exceptionPreprocess + 165   1   libobjc.A.dylib   
0x000000010d96bdeb objc_exception_throw + 48    2   CoreData            
0x000000010b81efff -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:]
+ 1583  3   CoreData 0x000000010b7117e3 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:]
+ 35    4   CoreData 0x000000010b8047b8 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:]
+ 344   5   CoreData 0x000000010b71167c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:]
+ 316   6   CoreData 0x000000010b7112f6 -[NSSQLCore newRowsForFetchPlan:]
+ 118   7   CoreData 0x000000010b710bac -[NSSQLCore objectsForFetchRequest:inContext:]
+ 524   8   CoreData 0x000000010b710549 -[NSSQLCore executeRequest:withContext:error:] 
+ 377   9   CoreData 0x000000010b7f5eaf __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke 
+ 3311  10  CoreData 0x000000010b7ff4dd gutsOfBlockToNSPersistentStoreCoordinatorPerform 
+ 189   11  libdispatch.dylib 0x000000010e4043eb _dispatch_client_callout 
+ 8     12  libdispatch.dylib 0x000000010e3e8ef5 _dispatch_barrier_sync_f_invoke 
+ 393   13  CoreData 0x000000010b7f05d5 _perform 
+ 197   14  CoreData 0x000000010b7101b4 -[NSPersistentStoreCoordinator
executeRequest:withContext:error:] 
+ 500   15  CoreData 0x000000010b70e973 -[NSManagedObjectContext executeFetchRequest:error:] 
+ 579   16  CoreData 0x000000010b7c65f5 -[NSManagedObjectContext executeRequest:error:] 
+ 421   17  Autos 0x000000010b652db9 _TFC5Autos14ViewController11viewDidLoadfT_T_ 
+ 3929  18  Autos 0x000000010b6531f2 _TToFC5Autos14ViewController11viewDidLoadfT_T_ 
+ 34    19  UIKit 0x000000010c58a984 -[UIViewController loadViewIfRequired] 
+ 1198  20  UIKit 0x000000010c58acd3 -[UIViewController view] 
...

我的视图控制器代码是:

let appDelegado:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let contexto:NSManagedObjectContext = appDelegado.managedObjectContext
let entidadMarca =  "Marca"
let entidadModelo =  "Modelo"

let modeloNike = "AIRFORCE 1"

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Conusltando Base de datos
        let newMarca = NSEntityDescription.insertNewObjectForEntityForName(entidadMarca, inManagedObjectContext:contexto)

        newMarca.setValue("NIKE", forKey: "nombre")

        print("nueva marca ----->\(newMarca) \n")
        //objeto modelo

        let newModelo = NSEntityDescription.insertNewObjectForEntityForName(entidadModelo, inManagedObjectContext:contexto)

        newModelo.setValue("AIRFORCE 1", forKey: "nombre")

        let entytiModelo = NSEntityDescription.entityForName(entidadMarca, inManagedObjectContext:contexto)

        let objArray = newMarca.mutableSetValueForKey("modelos")
        objArray.addObject(newModelo)
        print("Nuevo modelo:::::::: \(newModelo)")
        print("arreglo \(objArray)")
        saveInBD()
        print("Nuevo Marca \(newMarca)")

        let entityMarca = NSEntityDescription.entityForName(entidadMarca, inManagedObjectContext:contexto)
        let oderBy = NSSortDescriptor(key: "nombre", ascending: true)
        let consulta = NSFetchRequest(entityName:entidadMarca )
        consulta.sortDescriptors = [oderBy]
        let predicate  =  NSPredicate(format: "modelos = %@",modeloNike )
        consulta.entity = entityMarca
        consulta.predicate = predicate

        if let resultado = try?contexto.executeRequest(consulta){
            print("Resultado:::::::--->\(resultado)")

        }else{
            print("No hay Valores")
        }
    }
    func saveInBD() {
        do {
            try contexto.save() // Il metodo .save() dell'NSManagedObjectContext svolge questo compito.
        } catch let error {
            print((error as! NSErrorPointer).debugDescription)
        }
    }
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

看起来像:

  • 您正在获取实体Marca
  • 此抓取的谓词为NSPredicate(format: "modelos = %@",modeloNike)
  • modelos是一个多对多关系,将表示为一组
  • modeloNike是一个字符串

因此,您要求Core Data比较两个不同类型的东西,将一组与一个字符串进行比较。这是你的问题。