IOS 8 Swift NSFetchResult predicate with Core Data relationship cannot access internal identifier

Very new to Core Data, but I read that I can get data that uses an Entity relationship. Now, based on Mysql, maybe I'm making too many assumptions, but I have 2 objects configured with a relationship between them, and I can’t get the correct data.

Here are my 2 models:

@objc(Categories) class Categories: NSManagedObject { @NSManaged var category: String @NSManaged var exp: String @NSManaged var order: NSNumber } @objc(Techniques) class Techniques: NSManagedObject { @NSManaged var korean: String @NSManaged var order: NSNumber @NSManaged var spanish: String @NSManaged var categories: Categories } 

After I created the relation of methods to categories, Core Data added this field to Sqlite DB:

 ZCATEGORIES - INTEGER 

Let's say I want to get all the methods related to category # 3 (with internal ZID: 3)

If I do this:

 request.predicate = NSPredicate(format: "categories == %@", 3) 

It works. But if I do this:

 request.predicate = NSPredicate(format: "categories == %@", category.category) 

This does not work. I understand that this does not work, because category.category is a string, not an integer.

My question is:

Should I create my own field of relationship identifiers in the Category object and set it, and then call the method, for example:

 request.predicate = NSPredicate(format: "categories == %@", category.categoryID) 

?

Is there a way to access the internal category identifier to make this relation work?

Or, even better, it seems to me that in order to get this relationship there should be an internal mechanic, not writing a SQL-like query, but simply using Object, something like: Techniques.categores .

Thanks. I have not found a good answer anywhere.

+7
ios swift nsfetchedresultscontroller
source share
2 answers

You are missing the attribute name, so the predicate knows which attribute of the category class to compare. This way you do not need to use an internal unique identifier. Although it can be obtained (and will work), this is not the way to Core Data, which is trying to divert this information for you.

 let categoryPredicate = NSPredicate(format: "categories.category == %@", category.category) 

As mentioned, feedback is recommended. It allows Core Data to maintain a consistent graph of objects and can be used to move from category methods to them.

+7
source share

How did you decide a friend?

enter image description here

enter image description here

 import UIKit import CoreData let appdelegado:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let contexto2:NSManagedObjectContext = appdelegado.managedObjectContext class Consultar: UIViewController { let marca = Marca() let modelo = Modelo() override func viewDidLoad() { super.viewDidLoad() //Consultando con diferente contextp let entityMarca = NSEntityDescription.entityForName("Modelo", inManagedObjectContext: contexto2) let oderBy = NSSortDescriptor(key: "nombre", ascending: true) let consulta = NSFetchRequest(entityName: "Modelo") consulta.sortDescriptors = [oderBy] //???¿¿???¿¿???¿ HELP let predicate = NSPredicate(format: "modelo.marca = %@",) consulta.entity = entityMarca consulta.predicate = predicate //let arrayResultado:[NSArray] = try!contexto.executeFetchRequest(consulta) as! [NSArray] //if let resultado = try?contexto.executeFetchRequest(consulta) where resultado.count > 0{ if let arrayResultado:[NSArray] = try!contexto.executeFetchRequest(consulta) as! [NSArray] where arrayResultado.count > 0{ for x in arrayResultado { print("\n ====================DATOS RECUPERADOS======================") print(x) // print(x.valueForKey("nombre")!) // print(x.valueForKey("marca")!) } }else{ print("Sin modelos") } } } 
+1
source share

All Articles