0

I have two entities, User and Task. They are linker by a One to Many relationship, this is what they look like

import Foundation
import CoreData
@objc(User)
class User: NSManagedObject {
    @NSManaged var id: String
    @NSManaged var name: String
    @NSManaged var task: NSSet
}

and

import Foundation
import CoreData

@objc(Task)
class Task: NSManagedObject {

    @NSManaged var context: String
    @NSManaged var date: String
    @NSManaged var detail: String
    @NSManaged var folder: String
    @NSManaged var id: String
    @NSManaged var responsable: String
    @NSManaged var status: String
    @NSManaged var summary: String
    @NSManaged var user: User

}

The relationship on the xcdatamodel is:

relationship: user, destination: User, Inverse: task  for Task
relationship: task, destination: Task, Inverse: user  for User

I have my fetchedResultController that let me doing my requests on the Task table:

func getFetchedResultsController(String) -> NSFetchedResultsController{
    frc = NSFetchedResultsController(fetchRequest: taskFetchRequest(folder), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}

func taskFetchRequest(String) -> NSFetchRequest {
    //on choisit sur quel Entity on travaille
    let fetchRequest = NSFetchRequest(entityName: "Task")
    //on Choisit sur quel attribut on place l'Order By. Ici sur Summary en Ascendant
    let predicate = NSPredicate(format: "folder = %@", folder)
    let sortDescriptor = NSSortDescriptor(key: "summary", ascending: true)
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}

On both entity, I have one common attribute, id of User and responsable of Task are the same things, What I need to do is, for a task that has 135482dfsq4g1responsible, printing the corresponding name of the User entity (so the id 135482dfsq4g1)

I have already done some tests but nothing effective, like this:

var task : Task?
var user : User?
if(task?.responsable == user?.id){
        OwnerTextField.text = user?.name
    }else{
        println(task?.responsable)
        println(user?.id)
        OwnerTextField.text = ""
    }

task?.responsable is found but not user?.id, so I can't compare:

Optional("76bbfe695318d471a541bc3333e58eea28acae54")
nil

Any help would be appreciated.

Regards.

fselva
  • 457
  • 1
  • 7
  • 18
  • Why do you have a responsible property and a user relationship? Can a task have John as user end Anna as responsible? – Mario Zannone Jul 10 '15 at 15:52
  • a task has one responsible (one user) but a user can have severals tasks. user is a table that contains the name and the id of the user. task only has the user id. I need to print the user name instead of the id, that's why I do this relationship, is it a mistake? – fselva Jul 10 '15 at 15:57

1 Answers1

1

Just remove the responsible property and rename the relationship user as responsible. You will get

NSManaged var responsable: User

and getting responsible?.name (and responsible?.id if you really need it) will be straightforward.

Mario Zannone
  • 2,843
  • 12
  • 18